gpt4 book ai didi

javascript - 运行 Jest 测试时遇到问题 : `SyntaxError: Unexpected token <`

转载 作者:行者123 更新时间:2023-11-28 21:24:01 24 4
gpt4 key购买 nike

我知道这可能之前已经发布过,但我真的很困惑为什么我的测试在我的 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 设置

  "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: Unexpected token <` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com