How do I specify module paths for Jest tests?
At the moment Jest doesn't support NODE_PATH
in its module resolution code (for no reason other than the fact that it just wasn't ever built).
We're tracking the issue here for now: https://github.com/facebook/jest/issues/102
Ran into the same issue where Jest didn't respect webpack module resolve alias.
In jest.config.js
:
moduleNameMapper: {
'Services(.*)$': '<rootDir>/src/services/$1'
}
You can now use the following import statement:
import { validateEmail } from 'Services/utilities';
From Jest doc:
https://jestjs.io/docs/en/configuration
in package.json
define:
"jest": {
"modulePaths": [
"<rootDir>/src/"
],
},
Jest does not read NODE_PATH. In our project where we use webpack we faced problems with alias
and moduleDirectories
. Jest didn't resolve paths correctly. So we solved that like this:
Suppose you want to import
import SomeComp from 'components/SomeComp';
which is located in /src/components/SomeComp/index.js
In package.json
define:
"jest": {
"moduleNameMapper": {
"/src/(.*)": "<rootDir>/src/$1"
}
},