更新Jest测试库后出现意外令牌(SyntaxError)



当使用Jest运行我的测试套件时,我遇到了要求我更新包的警告:

npm WARN deprecated jest-dom@2.1.1: 🚨 jest-dom has moved to @testing-library/jest-dom. Please uninstall jest-dom and install @testing-library/jest-dom instead, or use an older version of jest-dom. Learn more about this change here: https://github.com/testing-library/dom-testing-library/issues/260 Thanks! :)
npm WARN deprecated react-testing-library@5.9.0: 🚨  react-testing-library has moved to @testing-library/react. Please uninstall react-testing-library and install @testing-library/react instead, or use an older version of react-testing-library. Learn more about this change here: https://github.com/testing-library/dom-testing-library/issues/260 Thanks! :)

在package.json中,我更改了以下

"jest-dom": "^2.1.1",
"react-testing-library": "^5.3.0"

"@testing-library/jest-dom": "^5.11.1",
"@testing-library/react": "^10.4.7"

当然还有的进口声明

import "jest-dom/extend-expect";

import "@testing-library/jest-dom";

等等。

在我删除旧的并添加新的之后,我出现了多个错误,导致我的测试失败(仅在我的Semaphore CI设置中,而不是在我的本地机器上(。

FAIL src/redux/actions/tests/myActions.test.js
● 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/semaphore/my-app/client/node_modules/@testing-library/dom/dist/helpers.js:44
} catch {// not using Jest's modern fake timers
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/@testing-library/dom/dist/pretty-dom.js:13:16)
at Object.<anonymous> (node_modules/@testing-library/dom/dist/config.js:11:18)

我不是一个前端开发人员,所以我很高兴听到需要更多的信息来提供帮助。非常感谢!

错误指的是可选的catch绑定,这是现代JS功能,自Node 10以来一直支持。这意味着@testing-library/dom包不支持旧的Node版本,这可以通过检查其package.json.中的engines部分来确认

一个更好的解决方案是更新Node.js,因为8已经到了生命的尽头。或者,如错误所示,该包可以降级为较低的主版本,或者通过在transformIgnorePatterns中列出它的白色列表进行传输。

@Estus的答案是绝对正确的,我投了赞成票。只是想添加实际的修复,如果你也在为你的CI使用Semaphore,所以你不需要像我那样花更多的时间进行调查。

  • 确保当前节点版本至少是最新的稳定LTS,并且本地使用的版本是测试通过的版本
  • 如果您还没有.nvrrc文件,则生成该文件:node -v > .nvmrc
  • 确保在信号量.yml的每个块中调用nvm use

即为您的项目使用外推版本:https://github.com/semaphoreci-demos/semaphore-demo-javascript/blob/master/.semaphore/semaphore.yml.

这将确保您的节点版本是同步的,并且应该解决任何";SyntaxError:如果相同的测试在本地通过,您在CI中遇到意外的令牌{quot;错误。如果您没有指定节点版本,Semaphore将使用v8.17.0作为默认版本(https://docs.semaphoreci.com/ci-cd-environment/ubuntu-18.04-image/#javascript-通过nodejs(!因此,任何没有指定版本的人在升级任何Jest-lib时都会遇到这个错误。

最新更新