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

moduleNameMapper doesn't handle multiple paths #1072

Closed
jakeleboeuf opened this issue Apr 17, 2019 · 6 comments · Fixed by #1690
Closed

moduleNameMapper doesn't handle multiple paths #1072

jakeleboeuf opened this issue Apr 17, 2019 · 6 comments · Fixed by #1690

Comments

@jakeleboeuf
Copy link

Issue :

I'm having trouble configuring jest to resolve modules when using multiple paths in my tsconfig. It seems ts-jest will only resolve a single path- is this the case?

Expected behavior :

I would expect to find some solution to resolve paths when my tsconfig.json paths array has more than one path listed. After all, it is an array ¯_(ツ)_/¯

File structure is as follows
.
├── tsconfig.json
├── jest.config.js
├── src-common
|   └── components
|       └── Button.tsx
├── src-admin
|   └── components
|       └── Login.tsx
└── src-main
    └── components
        └── Share.tsx
relevant parts of tsconfig.json
{
  "baseUrl": ".",
  "paths": {
    "@components/*": ["src-common/components/ui/*", "src-admin/components/ui/*", "src-main/components/*],
  }
}
jest.config.js
module.exports = {
  setupFilesAfterEnv: ["react-testing-library/cleanup-after-each"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  moduleNameMapper: {
    // Resolves a single path as expected 👍 
    "^@components/(.*)$": "<rootDir>/src-common/components/$1",

    // No regex in path 😢 
    "^@components/(.*)$": "<rootDir>/src-{common,admin,main}/components/$1",

    // No array in path 😢
    "^@components/(.*)$": [
      "<rootDir>/src-common/components/$1"
      "<rootDir>/src-admin/components/$1"
      "<rootDir>/src-main/components/$1"
    ],
  },
};

Debug log:

log file content
# content of ts-jest.log :
# Project is more complex than this example, and the output doesn't exactly match ☝️ 

Minimal repo :

Unfortunately Codesandbox doesn't support tsconfig.json paths :(

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 22, 2019

moduleNameMapper is jest configuration itself. I don't think ts-jest did something which changed the default behavior of this config. I'd suggest to check with jest community about your issue.

@OrkhanAlikhanov
Copy link
Contributor

Related to jestjs/jest#8461

@OrkhanAlikhanov
Copy link
Contributor

@ahnpnl

Since jestjs/jest#9465 has been merged and released in jest 25.2.0 and onwards this feature can be implemented in pathsToModuleNameMapper util. I can create a PR for this, here's how I think it should be implemented:
if jest version is less than 25.2.0 we warn that only first item will be used as this version of jest does not support array paths, otherwise we create array of paths. If agreed I'll create a PR with test cases

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 26, 2020

hi, thanks for coming back to this. I agree to the approach. Besides, actually ts-jest 26 is only compatible with jest 26 (theoratically it can work with jest 25) so I wonder if it is needed to have the check version less than 25.2.0 unless ts-jest needs to have the support for ts-jest 25.x

@kulshekhar what do you think ? The PR jestjs/jest#8461 was merged in jest 25 so in theory, we should support this feature in ts-jest 25.x. But now we are at 26.x so there are 2 ways to go for the PR of @OrkhanAlikhanov

  • We want to support this feature for ts-jest 25.x: the PR should target both master and a branch which is cloned from 25.5.1 and @OrkhanAlikhanov can use the check version less than 25.2.0
  • We only want to support this feature for ts-jest26.x: the PR should only target master and @OrkhanAlikhanov doesn't need to add the check version less than 25.2.0

@kulshekhar
Copy link
Owner

I think those who'll start using jest now will start with the latest version and those who are already on v25.x, will likely have a setup that will keep working.

It might make sense to start with doing this only for 26.x for now and if there are requests for this feature in 25.x, we add it there. What do you think?

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 27, 2020

Sound good to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants