我知道这可能是以前发布的,但是我真的对为什么我的测试无法在我的React项目中工作感到困惑。这是我的.babelrc
文件:
{
"presets": ["react", "es2015", "stage-0"],
"plugins": [
"transform-runtime",
"add-module-exports",
"transform-decorators-legacy"
],
"env": {
"development": {
"plugins": [
"typecheck",
["react-transform", {
"transforms": [{
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"]
}]
}]
]
},
"test": {
"presets": ["es2015", "react", "stage-0"],
"plugins": [
"transform-runtime",
"add-module-exports",
"transform-decorators-legacy"
]
}
}
}
这是我在
package.json
中的笑话设置 "jest": {
"moduleFileExtensions": [
"js",
"jsx",
"json"
],
"moduleNameMapper": {
"ExampleTest": "<rootDir>/app/pages/ExampleTest.js"
},
"moduleDirectories": [
"app",
"node_modules"
]
},
我收到的错误是
Test suite failed to run... SyntaxError: Unexpected token <
。从我能说出的错误来看,它试图运行的文件中包含JSX / ES6代码...而且它不知道该如何转译?这很奇怪,因为我已经在
.babelrc
中告诉它这样做。我还为Jest提供了适当的moduleDirectories
。任何帮助将不胜感激。
最佳答案
JSON中的最后一个逗号是错误的。它应该看起来像:
{"root":{
"presets": ["react", "es2015", "stage-0"],
"plugins": [
"transform-runtime",
"add-module-exports",
"transform-decorators-legacy"
],
"env": {
"development": {
"plugins": [
"typecheck",
["react-transform", {
"transforms": [{
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"]
}]
}]
]
},
"test": {
"presets": ["es2015", "react", "stage-0"],
"plugins": [
"transform-runtime",
"add-module-exports",
"transform-decorators-legacy"
]
}
}
}}
关于javascript - 无法运行Jest测试:`SyntaxError:意外 token <`,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45246815/