ts-jest: moduleNameMapper doesn't work for me
I change the path configuration in my tsconfig, for better paths. But now i cannot execute my testcases. So i checked the following documentaries:
Is that an error or a misconfiguration? By Error i will do a small repo.
Snippet tsconfig.json
...
"rootDirs": [
"src/",
"test/"
],
"baseUrl": ".",
"paths": {
"log": ["src/util/log"],
"server": ["src/server"],
"util/*": ["src/util/*"],
"api/*": ["src/api/*"],
"middleware/*": ["src/middleware/*"],
"service/*": ["src/service/*"],
"types/*": ["src/types/*"],
"test/*": ["test/*"],
"mocks/*": ["test/mocks/*"],
"src/*": ["src/*"]
},
Snippet package.json
...
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
...
"moduleDirectories": [
".",
"node_modules"
],
"moduleNameMapper": {
"log": "<rootDir>/src/util/log",
"server": "<rootDir>/src/server",
"util/(.*)": "<rootDir>/src/util/$1",
"api/(.*)": "<rootDir>/src/api/$1",
"middleware/(.*)": "<rootDir>/src/middleware/$1",
"service/(.*)": "<rootDir>/src/service/$1",
"types/(.*)": "<rootDir>/src/types/$1",
"test/(.*)": "<rootDir>/test/$1",
"mocks/(.*)": "<rootDir>/test/mocks/$1",
"src/(.*)": "<rootDir>/src/$1"
}
...
ERROR
● Test suite failed to run
Configuration error:
Could not locate module ../util/params (mapped as C:\Users\XXX\backend\src\util\params)
Please check:
"moduleNameMapper": {
"/util\/(.*)/": "C:\Users\XXX\backend\src\util\$1"
},
"resolver": undefined
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 30 (2 by maintainers)
Commits related to this issue
- feat(config): adds a helper to build moduleNameMapper from paths Closes #364 — committed to kulshekhar/ts-jest by huafu 6 years ago
I almost gave up on this but finally fixed it.
The key below was that
moduleDirectorieswas not required to get it to work butmodulePathsis. Also a piece of advice is to use<rootDir>instead of./.Changing the
moduleDirectorieskey in the jest configuration got this working (I tested withjest --no-cachefrom the root directory)Hope this helps
This should not have been closed. There is an opportunity to automate this so that developer does not have to touch jest.config.
If you’re using a different baseUrl in your tsconfig.json, you should set modulePaths to that baseUrl.
I have this problem that should be related to this issue. When I call a module “util” that links to
src/utilfolder, it doesn’t work.And throws:
But when I rename the folder and the module to “utils”, in plural… It works.
🤷♂️🙄
yup @DmitryEfimenko that is what I meant by being an opt-in.
Example of where you would not want it:
And that is not the only project I work on where both don’t match 😉
But yeah, having an option for it is a good idea. It could be a helper so that people can customize it:
path mapping works depending on your
rootDir, see https://github.com/kulshekhar/ts-jest/blob/master/TROUBLESHOOTING.mdI’m struggling with this as well. I’m using an nrwl/nx workspace, the paths in tsconfig.app allow the app to run fine, I put the module name mappings in jest.config, nothing. I’ve tried using the jest utils that do the path to module mappings from tsconfig.app prefixed with rootDir, explicitly putting the entries (so not pulling them in from tsconfig.app), and a hundred other things. Nothing works. You run the tests and any module mapped to a path is not found. The one and only solution I have that works is just to remove the paths and start typing ‘…/…/…/…’ everywhere (which would be dozens of imports).
I have a new problem
my tsconfig.json is configured with
when I use in Jest.config.ts
raises a exception:
how do I resolve this?
My dependencies:
Wish this was automated 🙈 🤦 @DarkLite1 Your solution works like a charm, many thanks!
i hope u can understand the problem and reproduce the issue or the misconfig
repo
Thanks for ur help
Hi there!
I’m experiencing the warning
Mapping only to first target of "*" because it has more than one (2)when I usepathsToModuleNameMapperHaving multiple path mapping for a pattern is supported by typescript (see https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping). Is there a reason we can’t do that here?
Anyone test this on windows? I can’t seem to get this to work for windows.
I’d think it’s a rare case when you would not want it. What if it was behind an option?