The cleanest solution I found to ignore the required module is to use moduleNameMapper config (works with the latest version 0.9.2)
The documentation is hard to follow. I hope the following helps.
Add the moduleNameMapper key to the packages.json configuration. The key for the item must be a regular expression of the desired string. Example with ".less" files:
"moduleNameMapper": { "^.*[.](less|LESS)$": "EmptyModule" },
Add EmptyModule.js to the root folder:
module.exports = '';
The comment is important because moduleNameMapper uses the EmptyModule as an alias for this module ( more about providing a Module).
Now each of them requires a link that matches the regular expression will be replaced with an empty string.
If you are using the moduleFileExtensions configuration with the "js" file, be sure to also add the EmptyModule to your "unmockedModulePathPatterns".
Here is the jest configuration I ended up with:
"jest": { "scriptPreprocessor": "<rootDir>/node_modules/babel-jest", "moduleFileExtensions": ["js", "json","jsx" ], "moduleNameMapper": { "^.*[.](jpg|JPG|gif|GIF|png|PNG|less|LESS|css|CSS)$": "EmptyModule" }, "preprocessorIgnorePatterns": [ "/node_modules/" ], "unmockedModulePathPatterns": [ "<rootDir>/node_modules/react", "<rootDir>/node_modules/react-dom", "<rootDir>/node_modules/react-addons-test-utils", "<rootDir>/EmptyModule.js" ] }
ZahiC Mar 21 '16 at 17:22 2016-03-21 17:22
source share