Do you want to request a feature or report a bug?
Feature
What is the current behavior?
I'm using the Webpack resolver for my code and I omit module extensions (in fact, I'm using eslint's import/extensions to make sure they aren't used), but I discovered that moduleNameMapper works on the path before resolving.
So if I write this:
import './style'
and use this configuration:
"moduleNameMapper": {
"\\.(s?css)$": "<rootDir>/src/__mocks__/scss.js"
}
My styles are imported as normal instead of the mock.
What is the expected behavior?
It would be nice if there were an option that allowed by to mock files by resolved extension somehow. For example if moduleNameMapper resolved the original import file name, then applied the mapping to that, or if there were a separate configuration option like "moduleExtensionMapper" that replaced all imports for a given extension with the provided mock.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Shouldn't matter, but here they are:
"jest": {
"testMatch": [
"**/*.spec.js?(x)"
],
"moduleDirectories": [
"node_modules",
"src"
],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"scss"
],
"moduleNameMapper": {
"\\.(s?css)$": "<rootDir>/src/__mocks__/scss.js"
},
"collectCoverageFrom": [
"src/**/*.js?(x)",
"!src/index.jsx"
],
"coverageReporters": [
"lcovonly"
]
}
Jest 20.0.4
Node 8.4.0
NPM 5.4.1
Yep, moduleNameMapper works on the string before resolution. This is the expected behavior. You can provide a custom resolver to Jest if you prefer that, and build your own :)
@cpojer is there some documentation about how we would provide a custom resolver as mentioned? And just to be real clear, the moduleNameMapper will not be able to target CSS module imports if the import statement does not explicitly include the ".css" in the path?
@gpartida, FYI I've re-raised this issue in #7271. Have a look.
Most helpful comment
@cpojer is there some documentation about how we would provide a custom resolver as mentioned? And just to be real clear, the moduleNameMapper will not be able to target CSS module imports if the import statement does not explicitly include the ".css" in the path?