gpt4 book ai didi

javascript - 语法错误 : Unexpected token ( for jest

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:25:43 26 4
gpt4 key购买 nike

在设置了一些 postcss 和 webpack 之后,我的一些 jest 测试失败了。都因同样的错误而失败。非常奇怪的错误,似乎 Jest 甚至不能识别箭头函数而不是 async/await

FAIL  tests/backend/integration/models/threadsModel.test.js
● Test suite failed to run

/Users/albertgao/codes/node/projectTalk/tests/backend/integration/models/threadsModel.test.js:115
test('Should return empty list when no data could be fetched', async () => {
^
SyntaxError: Unexpected token (

at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)

测试简单,之前通过:

test('Should return empty list when no data could be fetched', async () => {
const result = await ThreadModel.getThreads(threads[19], 5);
expect(result).toHaveLength(0);
});

我试过:

  • --无缓存
  • 删除node_modules 并重新安装所有模块

仍然有这个错误。

这是我调用测试的方式:

"NODE_ENV=test ./node_modules/.bin/jest --no-cache --config=configs/jest.config.json",

这是我在 ./configs/ 文件夹中的 jest.config.js:

{
"verbose": true,
"rootDir": "../",
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/tests/acceptance/", "<rootDir>/tests/coverageReport/", "<rootDir>/dist/"],
"collectCoverage": false,
"coverageDirectory": "tests/coverageReport",
"collectCoverageFrom" : ["**/src/**"],
"coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/configs/", "<rootDir>/src/config/appconfig.json", "<rootDir>/dist/"],
"coverageReporters": ["text", "text-summary"]
}

这是我在 ./configs/ 文件夹中的 .babelrc:

{
"presets": [["env",{"modules":false}], "react"],
"plugins": ["react-hot-loader/babel"],
"ignore": [
"tests/",
"dist/",
"node_modules/",
"src/backend/",
"public"
]
}

我用的是最新的Jest

有什么解决办法吗?

最佳答案

因为异步箭头函数ES2017中被支持.所以你必须配置一个javascript转译器,例如:Babel .

 npm install --save-dev babel-jest regenerator-runtime babel-preset-stage-0

.babelrc

{
"presets": ["es2015", "stage-0"]
}

您可以在 Additional Configuration 进一步查看 jest 配置.

关于javascript - 语法错误 : Unexpected token ( for jest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44743754/

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