moduleNameMapper doesn't handle multiple paths
See original GitHub issueIssue :
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:
# 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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
Sound good to me 👍