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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not friendly with custom paths. #908

Closed
mshima opened this issue Apr 21, 2021 · 3 comments 路 Fixed by #910
Closed

Not friendly with custom paths. #908

mshima opened this issue Apr 21, 2021 · 3 comments 路 Fixed by #910
Labels
馃悰 Bug Confirmed Bug is confirmed

Comments

@mshima
Copy link

mshima commented Apr 21, 2021

馃悰 Bug Report

Using jest-preset-angular have unexpected results with webapp path different of src.

jest-preset-angular should not provide src hardcoded values at moduleNameMapper:

moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/src/environments/$1',
},

Instead, it should provide default values for roots and modulePaths:

  roots: ['<rootDir>', '<rootDir>/src'],
  modulePaths: ['<rootDir>/src'],

To Reproduce

Steps to reproduce the behavior:

Jest fails with:

    Configuration error:

    Could not locate module app/config/input.constants mapped as:
    /Users/mshima/bug-tracker-tsconfig/src/app/$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^app\/(.*)$/": "/Users/mshima/bug-tracker-tsconfig/src/app/$1"
      },
      "resolver": undefined
    }

Thats because moduleNameMapper takes precedence over modulePaths.

npm test -- --show-config (relevant parts):

      "moduleNameMapper": [
        [
          "\\.(jpg|jpeg|png)$",
          "/Users/mshima/bug-tracker-tsconfig/node_modules/@angular-builders/jest/dist/jest-config/mock-module.js"
        ],
        [
          "^src/(.*)$",
          "/Users/mshima/bug-tracker-tsconfig/src/$1"
        ],
        [
          "^app/(.*)$",
          "/Users/mshima/bug-tracker-tsconfig/src/app/$1"
        ],
        [
          "^assets/(.*)$",
          "/Users/mshima/bug-tracker-tsconfig/src/assets/$1"
        ],
        [
          "^environments/(.*)$",
          "/Users/mshima/bug-tracker-tsconfig/src/environments/$1"
        ]
      ],
      "modulePathIgnorePatterns": [],
      "modulePaths": [
        "/Users/mshima/bug-tracker-tsconfig/src/main/webapp"
      ],

Expected behavior

Jest should find the relevant modules.

Link to repo (highly encouraged)

Error log:

# content of error stacktrace :

envinfo

System:
    OS:

Npm packages:
    jest:
    jest-preset-angular:
    typescript:
@mshima mshima added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Apr 21, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 21, 2021

The configuration in our preset works with basic Angular project generated by Angular CLI. You can always override the configuration on your own for moduleNameMapper.

I think the best is just removing moduleNameMapper from the preset which makes everyone happy :)

@ahnpnl ahnpnl added 馃悰 Bug Confirmed Bug is confirmed and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Apr 21, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 21, 2021

Since we are going to have 9.0.0 release soon so that change can be included in 9.0.0 as breaking change.

@mshima
Copy link
Author

mshima commented Apr 21, 2021

Thanks for the quick answer.

Manually replacing at node_modules/jest-preset-angular/jest-preset.js, moduleNameMapper with

  roots: ['<rootDir>', '<rootDir>/src'],
  modulePaths: ['<rootDir>/src'],

The project customs roots and modulePaths applies and npm test succeeds.

The workaround it to override each moduleNameMapper with a custom one.

ahnpnl added a commit that referenced this issue Apr 28, 2021
Closes #908 

BREAKING CHANGE
When generating a new project from Angular CLI, by default the `tsconfig.json` doesn't contain any path mappings hence removing `moduleNameMapper` from preset will make sure that the preset works in pair with `tsconfig.json`.

Ones who are relying on the value of `moduleNameMapper` from the preset should create their own `moduleNameMapper` config manually or via `ts-jest` util https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
馃悰 Bug Confirmed Bug is confirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants