我已经为连接的组件创建了测试。在一个文件中,我导入位于项目根文件夹外部的“本地化”模块。它没有编译到ES5,并且Jest报告了SyntaxError:意外的令牌导出。如何强制Jest转换该文件?
1 | import axios from 'axios';
2 | import * as types from './actionTypes';
error > 3 | import Localization from '../../../../React/MainMethods/localization';
我的.babelrc带有env预设。
{
"presets": ["env", "react"]
}
package.json
//...
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^23.4.0",
"babel-loader": "^7.1.2",
"babel-plugin-lodash": "^3.3.2",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"chalk": "^2.3.0",
"cross-env": "^5.1.1",
"css-loader": "^0.23.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-15": "^1.0.6",
"jest": "^23.4.1",
"jest-cli": "^23.4.1",
"parallel-webpack": "^1.5.0",
"progress-bar-webpack-plugin": "^1.11.0",
"react-test-renderer": "^15.6.2",
"redux-immutable-state-invariant": "^2.1.0",
"redux-mock-store": "^1.5.3",
"style-loader": "^0.13.1",
"webpack": "^4.16.1",
"webpack-cli": "^3.0.8"
},
//...
答案 0 :(得分:1)
如果您的项目中有babel.config文件,Jest将自动将文件转换为ES6。如果您想要更好的控制,可以将Jest键添加到package.json文件中。
"jest": {
"transform": {}
}
另外,看看Babel-Jest项目,该项目将在测试期间编译ES6模块。
https://github.com/facebook/jest/tree/master/packages/babel-jest#setup