Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest 27 - Paths not being honored #938

Closed
michaelfaith opened this issue May 28, 2021 · 5 comments · Fixed by #939
Closed

Jest 27 - Paths not being honored #938

michaelfaith opened this issue May 28, 2021 · 5 comments · Fixed by #939

Comments

@michaelfaith
Copy link
Contributor

🐛 Bug Report

We have a project with an Angular library, and are using tsconfig paths to help with import paths for components in the library. This worked great before the update, but is now failing with "Cannot find module" for all of the imports involving components from the library.

Snippet of our tsconfig

"paths": {
      "@fabric/components/*": [
        "./dist/components/*"
      ]
    }

Error log:

# content of error stacktrace :
 FAIL  src/app/component-demos/component-demos.component.spec.ts
  ● Test suite failed to run

    Cannot find module '@fabric/components/column-configuration-dialog' from 'src/app/component-demos/component-demos.component.spec.ts'

      38 | import { MatTreeModule } from '@angular/material/tree';
      39 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    > 40 | import { FabColumnConfigurationDialogModule } from '@fabric/components/column-configuration-dialog';
         | ^
      41 | import { FabConfirmationDialogModule } from '@fabric/components/confirmation-dialog';
      42 | import { FabGmLogoModule } from '@fabric/components/gm-logo';
      43 | import { FabMultiselectModule } from '@fabric/components/multiselect';

      at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:311:11)
      at Object.<anonymous> (src/app/component-demos/component-demos.component.spec.ts:40:1)

envinfo

System:
    OS: Windows 10

Npm packages:
    jest: 27.0.1
    jest-preset-angular: 9.0.0
    typescript: 4.2.6
@michaelfaith michaelfaith added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels May 28, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented May 28, 2021

This is because paths is tsconfig while Jest doesn't know that. You need to configure moduleNameMapper to tell Jest where to look, see our documentation here https://thymikee.github.io/jest-preset-angular/docs/guides/absolute-imports

@ahnpnl ahnpnl closed this as completed May 28, 2021
@ahnpnl ahnpnl added Not An Issue Not jest-preset-angular issue and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels May 28, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented May 28, 2021

An example is

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  moduleNameMapper: {
    ...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
    '^lodash-es/(.*)$': '<rootDir>/node_modules/lodash/$1',
  },
};

@michaelfaith
Copy link
Contributor Author

Did that change in 27? Because that worked fine with our setup previously. I'll give that a try. Thanks

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 28, 2021

That is related to #910 which is listed as a breaking change, you can reuse the previous configuration is https://github.com/thymikee/jest-preset-angular/blob/v8.4.0/jest-preset.js

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 28, 2021

I will update docs as well as CHANGELOG so it's clear about this breaking change.

@ahnpnl ahnpnl added Documentation 📖 and removed Not An Issue Not jest-preset-angular issue labels May 28, 2021
@ahnpnl ahnpnl reopened this May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants