我试着设置我的jest运行程序来正确地使用scss @impport语句(在我的React应用程序中的css模块中)。但不幸的是,每次运行测试脚本时,我都会得到一个错误。我尝试了不同的解决方案,与StackOverflow类似的票,但它没有帮助我。
错误示例:

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文件:
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'
],
});发布于 2020-02-02 14:29:11
不要在模块名mapper regex中使用空格。例如,而不是:
moduleNameMapper: {
'\\.(css | scss)$': 'identity-obj-proxy'
},用这个:
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy'
},我从未使用过身份-obj-代理,也没有测试过它,但正如我所读到的,它对普通文件映射程序执行类似的任务,如下所示:
const path = require('path');
module.exports = {
process(src, filename, config, options) {
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
},
};https://stackoverflow.com/questions/60026804
复制相似问题