首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新Jest测试库后的意外令牌(SyntaxError)

更新Jest测试库后的意外令牌(SyntaxError)
EN

Stack Overflow用户
提问于 2020-07-18 11:42:43
回答 2查看 5.4K关注 0票数 5

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

代码语言:javascript
复制
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中,我更改了以下内容

代码语言:javascript
复制
"jest-dom": "^2.1.1",
"react-testing-library": "^5.3.0"

代码语言:javascript
复制
"@testing-library/jest-dom": "^5.11.1",
"@testing-library/react": "^10.4.7"

当然,从

代码语言:javascript
复制
import "jest-dom/extend-expect";

代码语言:javascript
复制
import "@testing-library/jest-dom";

等。

在删除旧的和添加新的后,我得到了多个错误,使我的测试失败(只在我的信号量CI设置,而不是在我的本地机器上)。

代码语言:javascript
复制
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)

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-18 17:29:11

该错误指的是optional catch binding,它是现代JS特性,支持自Node 10。这意味着@testing-library/dom包不支持较旧的Node版本,这可以通过在其package.json中检查engines部分来确认。

一个更好的解决方案是更新Node.js,因为8已经到了生命的尽头。或者,可以将包降级为较低的主要版本,或者将其转到transformIgnorePatterns中的白色列表中,就像错误提示的那样。

票数 8
EN

Stack Overflow用户

发布于 2020-08-29 00:42:47

@Estus的回答是完全正确的,我投了赞成票。只是想添加实际的修复,如果你也使用信号量作为你的CI,所以你不需要花更多的时间调查,像我一样。

确保您的当前节点版本至少是最新的稳定的LTS,并且您在本地使用的版本是您的测试通过的版本,如果您还没有semaphore.yml.的

  • ,则生成一个.nvmrc文件:确定您在中调用nvm use每个

也就是说,使用外推法对你的项目,版本这个:https://github.com/semaphoreci-demos/semaphore-demo-javascript/blob/master/.semaphore/semaphore.yml

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62968136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档