更新 Jest 测试库后出现意外标记(SyntaxError)

2024-02-12

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

npm WARN deprecated [email protected] /cdn-cgi/l/email-protection: ???? 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 [email protected] /cdn-cgi/l/email-protection: ????  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"

to

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

当然还有来自的导入声明

import "jest-dom/extend-expect";

to

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

etc.

在删除旧的并添加新的后,我遇到了多个错误,导致我的测试失败(仅在我的 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 binding https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch,这是现代 JS 功能,自 Node 10 起就受支持。这意味着@testing-library/dom包不支持旧的 Node 版本,这可以通过检查来确认engines其 package.json 中的部分。

更好的解决方案是更新 Node.js,因为 8 已达到生命周期的终点。或者,可以将软件包降级到较低的主要版本,或者通过将其列入白名单来进行转译transformIgnorePatterns,如错误所示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新 Jest 测试库后出现意外标记(SyntaxError) 的相关文章

随机推荐