question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

moduleNameMapper doesn't handle multiple paths

See original GitHub issue

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 😦

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kulshekharcommented, May 27, 2020

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?

0reactions
ahnpnlcommented, May 27, 2020

Sound good to me 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest when multiple paths share the same tsconfig ...
How would one configure the moduleNameMapper inside of the jest.config.js file to allow multiple paths sharing the same alias, such as the ...
Read more >
Paths mapping | ts-jest - GitHub Pages
If you use "baseUrl" and "paths" options in your tsconfig file, you should make sure the "moduleNameMapper" option in your Jest config is ......
Read more >
jest: handling static assets with mocks | /*code-comments*/
In its simplest form, handling static assets is a two-step process: Update the Jest config using a moduleNameMapper; Create a mock file that ......
Read more >
Configuring Jest
When the projects configuration is provided with an array of paths or glob patterns, Jest will run tests in all of the specified...
Read more >
Resolve | webpack
import Test1 from 'xyz'; // Exact match, so path/to/file.js is resolved and ... If multiple files share the same name but have different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found