我正在尝试使用Jest为我为Rails应用编写的React前端设置一些测试。但是,当我尝试运行一个简单的测试时,在导入语句之后,Jest失败,出现意外标识符。
blah.js
const fun = () => "fun";
export default fun;
blah.test.js
const x = 4;
import fun from './blah';
it('is fun', () => {
fun();
});
yarn test
失败,并显示
import fun from './blah';
^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
我在测试文件中导入之前插入了const
声明,以查看它是否不能接受ES2015,但是由于通过了,所以我认为babel没问题。
我看到很多有关“意外的令牌导入”错误的问题-这似乎有所不同,因为它不是在抱怨导入令牌,而是在抱怨导入令牌。
关于如何克服这个问题的任何建议?
作为参考,这是我的配置文件:
.babelrc
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true
},
"useBuiltIns": true
}
],
"react"
],
"plugins": [
"syntax-dynamic-import",
"transform-object-rest-spread",
[
"transform-class-properties",
{
"spec": true
}
]
]
}
package.json
{
"dependencies": {
"@rails/webpacker": "3.5",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react_ujs": "^2.4.4"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.4.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"jest": "^23.4.1",
"webpack-dev-server": "2.11.2"
},
"scripts": {
"test": "jest"
},
"jest": {
"roots": [
"spec/javascript"
],
"moduleDirectories": [
"node_modules",
"app/javascript/components"
]
}
}
答案 0 :(得分:0)
好的,看来问题出在我的.babelrc
文件中。正确的版本是这样:
babel.rc
{
"presets": [ "env", "react" ]
}