我在 WebStorm IDE 中运行简单项目时遇到问题。这就是我运行时得到的结果:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/home/patryk/WebstormProjects/Case Converter/node_modules/hs-test-web/hstest/stage/stageTest.js:12
runner = new PureJsApplicationRunner();
^
SyntaxError: Unexpected token =
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/hs-test-web/hstest/index.js:1:110)
目前,我的项目仅包含 1 个 html 文件。我尝试重新安装nodejs和npm,但这不起作用
一天中的美好时光, 我在 WebStorm IDE 中的一个 Edu 项目中遇到了同样的问题 - 当我按下任务的“检查”按钮时 - 我遇到了问题: “测试套件运行失败 Jest 遇到了意外的令牌...”
如何解决:
{
"devDependencies": {
"@types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
}
}
您需要添加“jest”部分 - 请在下面找到完整的 package.json
{
"devDependencies": {
"@types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"verbose": true,
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"],
"testRegex": "test/.*\\.spec\\.ts$"
}
}
所以,希望对你有帮助。
如果它对某人有帮助:
我在运行笑话测试时遇到了同样的问题。我的例子中的问题是默认创建的错误运行配置。 Jest 包被选择为 /node_modules/react-scripts
当我将其更改为 /node_modules/jest 时,一切都开始工作了。