Reputation: 2463
I tried to set up my jest runner to work properly with scss @impport
statatement (inside css modules in my React app).
but unfortunately, I get an error each time I run a test script. I tried different solutions from the StackOverflow similar tickets, but it didn't help me.
example of my error:
my jest.config.js:
module.exports = {
transform: {
"^.+\\.(jsx?|tsx?)$": "<rootDir>/.jest/transform.ts",
},
testURL: 'http://localhost:6006/',
testRegex: "((\\.|/)(test|spec))\\.(jsx?|tsx?)$",
preset: 'jest-puppeteer',
moduleFileExtensions: [
"scss",
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
moduleDirectories: ["node_modules", "src"],
moduleNameMapper: {
'\\.(jpg | ico | jpeg | png | gif | eot | otf | webp | svg | ttf | woff | woff2 | mp4 | webm | wav | mp3 | m4a | aac | oga)$ ': '<rootDir>/__mocks__/fileMock.js',
'\\.(css | scss)$': 'identity-obj-proxy'
},
transformIgnorePatterns: ['<rootDir>/node_modules'],
setupFilesAfterEnv: [
"<rootDir>/.jest/register-context.ts",
"<rootDir>/.storybook/setupTests.ts"
]
};
transform.ts file:
module.exports = require('babel-jest').createTransformer({
presets: [
['@babel/preset-env', { targets: { node: 'current' }, modules: 'commonjs' }],
'@babel/preset-react',
'@babel/preset-typescript',
],
plugins: [
'require-context-hook',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-modules-commonjs'
],
});
Upvotes: 4
Views: 8590
Reputation: 840
Don't use spaces in the module name mapper regex. For example, instead of:
moduleNameMapper: {
'\\.(css | scss)$': 'identity-obj-proxy'
},
Use this:
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy'
},
I've never used identity-obj-proxy, I didn't tested it, but as I could read about, it does a similar task of a common file mapper like this:
const path = require('path');
module.exports = {
process(src, filename, config, options) {
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
},
};
Upvotes: 9