Here is the demo repo: https://github.com/cloud-walker/jest-module-resolver-bug steps to reproduce the bug are listed in the readme file.
What is the current behavior?
jest --watch -o will ignore deps imported using babel-plugin-module-resolver
What is the expected behavior?
I'm not sure if its possible, maybe I have to use the moduleNameMapper trick to make it work instead, but maybe it could work, so I'm notifying it.
Have the same issue
Jest will not make assumptions on aliases based on the babel plugins you use. You'll need to tell Jest how to resolve these absolute aliases using moduleNameMapper:
"jest": {
"moduleNameMapper": {
"@/(.*)": "<rootDir>/source/$1"
}
}
You'll also need to remove empty jest.config.js because Jest prioritizes it over package.json
This is a little unfortunate if you have a monorepo structure with aliases. If each package uses the ~ prefix to signify the root of the package for imports then Jest will get confused when trying to resolve those imports in sibling packages.
If instead jest could honor the .babelrc in each package / transpile the package before running then it would be able to resolve aliases in sibling packages.
Right now I have to manually build (run babel) before running my tests so my alias imports get converted to relative imports and jest can resolve them correctly.
Happy to give more info if you have any questions about this setup.
Interesting solution Daniel, but it defeat part of the performance of jest
I guess T_T
Il giorno gio 21 giu 2018 alle ore 23:16 Daniel Kezerashvili <
[email protected]> ha scritto:
This is a little unfortunate if you have a monorepo structure with
aliases. If each package uses the ~ prefix to signify the root of the
package for imports then Jest will get confused when trying to resolve
those imports in sibling packages.If instead jest could honor the .babelrc in each package / transpile the
package before running then it would be able to resolve aliases in sibling
packages.Right now I have to manually build (run babel) before running my tests so
my alias imports get converted to relative imports and jest can resolve
them correctly.Happy to give more info if you have any questions about this setup.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/facebook/jest/issues/5742#issuecomment-399246144, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABF1Cz6e3bxR2hP3OfWjrsVyNS2TM086ks5t_A1KgaJpZM4SgEJd
.
Another thought is for jest to look for the the jest config file relative to the file being imported and resolve the modules depending on its settings.
This way we don't necessarily lose the performance benefit but can have import resolution per package.
Hi to all, does anyone find a solution to this? I think i'm encountering the same issue. I keep getting the same error: error TS2307: Cannot find module
i'm using next.js, typescript, jest and enzyme and i also have installed the babel plugin module resolver.
why is this closed? I'm facing the same issue. It makes much more intuitive sense for alias configuration to be inherited from babel config when babel-jest is used
Most helpful comment
Jest will not make assumptions on aliases based on the babel plugins you use. You'll need to tell Jest how to resolve these absolute aliases using
moduleNameMapper:You'll also need to remove empty
jest.config.jsbecause Jest prioritizes it over package.json