Jest Paths to Modules failing when running tests
See original GitHub issueI have an NX workspace up and running, haven’t had any trouble with it in some time. However recently I created a new app, and have some paths configured in it in ts.config.app.json (I have no choice but to use these paths):
paths: {
"core/*": ["apps/myapp/myapp-ui/src/app/core/*"],
"shared/*": ["apps/,myapp/myapp-ui/src/app/shared/*"],
"templates/*": ["apps/myapp/myapp-ui/src/app/templates/*"],
}
or
paths: {
"^core/(.*)": ["apps/myapp/myapp-ui/src/app/core/1$"],
...
}
etc. (and many other options tried).
It works fines, runs as expected, etc, until I try to test it (using Jest). Then it’s all “modules not found”.
I have tried configuring modulePaths, moduleNameMapper, etc. etc. in the jest.config that lives in the root of the app, which looks like this:
workspace – apps ----someapps ------myapp ------jest.config.js
Nothing works, the modules are never found. I originally suspected that the rootDir is not doing what I’d expect it to be doing, but by “breaking” the jest resolver, it traces out the rootDir in the console (it actually tells you, rootDir = etc/etc), so I know it’s correct.
Any wisdom on this one would be greatly appreciated. I’m at a total brick wall here and I need to get these tests running. If I put the app in a vanilla angular 8 project, everything works fine.
Thanks in advance. I have looked elsewhere in the issues here, but there doesn’t seem, to be any basic guidance insofar as, “this is how it resolves the rootDir, which means this is how you need to configure it.”
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Hey folks, we have a special resolver that we use for Jest to find apps and libs in your workspace. Can you please make sure that the root jest.config.js has something like the following?
The most important piece being the
resolver
property.Awesome, thank you for sharing.
I did some research after my comment yesterday, turns out Jest doesn’t use ts-node or tsc when using globalSetup, which is why the paths aren’t loading. I was able to get it working by progamatically starting the services in my globalSetup file:
Hopefully this helps!