Hea*_*rts 12 javascript reactjs jest babel-jest
我花了很长时间查看有关此问题的其他问题并查看Github上的其他项目,但似乎没有任何答案对我有用.
我正在我的项目中加载第三方库,当运行Jest测试时,我收到错误
export default portalCommunication;
^^^^^^
SyntaxError: Unexpected token export
> 1 | import portalCommunication from 'mathletics-portal-communication-service';
Run Code Online (Sandbox Code Playgroud)
我已尝试以多种方式更新我的Jest配置以使其转换此库但我总是得到相同的错误.
这是我当前的jest.config.js文件:
module.exports = {
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy',
'\\.svg$': '<rootDir>/test/mocks/svg-mock.js'
},
setupFiles: ['./test/test-setup.js'],
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!mathletics-portal-communication-service)'
]
};
Run Code Online (Sandbox Code Playgroud)
我还尝试添加transform属性来对这个mathletics-portal-communication-service目录运行babel-jest.
请帮忙!
NJC*_*key 26
在我将 .babelrc 更改为 babel.config.js 之前,transformIgnorePatterns 对我不起作用,如下所示:
module.exports = {
"presets": [
"@babel/preset-env"
]
};
Run Code Online (Sandbox Code Playgroud)
如本评论所示:https : //github.com/facebook/jest/issues/6229#issuecomment-403539460
Roh*_*chi 13
我组织的另一个团队添加了一些通用模块。这没有被转译,所以出现了问题。
我遵循这个: https: //babeljs.io/docs/en/configuration#whats-your-use-case
.babelrc为babel.config.jsontransformIgnorePatterns: ['/node_modules/(?!(@companyName)/).*/'],这解决了我的问题。
作为现在的解决方法,我已更改我的配置以使用该moduleNameMapper选项为该库加载模拟类。我宁愿使用它,transformIgnorePatterns所以仍然会欣赏任何想法。
新配置:
module.exports = {
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy',
'\\.svg$': '<rootDir>/test/mocks/svg-mock.js',
'mathletics-portal-communication-service': '<rootDir>/test/mocks/mathletics-portal-communication-service-mock.js'
},
setupFiles: ['./test/test-setup.js']
};
Run Code Online (Sandbox Code Playgroud)
添加尾部斜杠为我解决这个问题:
{
// ...
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!mathletics-portal-communication-service/)'
]
};
Run Code Online (Sandbox Code Playgroud)