babeljs - Babel 7 更新搞砸了 Cannot use the decorators and decorators-legacy plugin 的 Jest 测试

标签 babeljs jestjs

我正在升级到 Babel 7,虽然应用程序正在加载,但我的测试出现了问题。他们以前很好。我从涉及装饰组件的每个测试中都收到此错误。

 FAIL  src/app/components/pages/Home/__tests__/Home.test.js
  ● Test suite failed to run

    Cannot use the decorators and decorators-legacy plugin together

      at validatePlugins (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10401:13)
      at getParser (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10464:5)
      at parse (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10448:12)
      at parser (node_modules/@babel/core/lib/transformation/normalize-file.js:170:34)
      at normalizeFile (node_modules/@babel/core/lib/transformation/normalize-file.js:138:11)
      at runSync (node_modules/@babel/core/lib/transformation/index.js:44:43)
      at transformSync (node_modules/@babel/core/lib/transform.js:43:38)
      at transform (node_modules/@babel/core/lib/transform.js:22:38)

.babelrc 请注意:我正在使用 remove-decorators 来运行我的测试,因为没有它就无法开 Jest 。您认为这可能是问题所在?

  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-object-rest-spread",
    ["@babel/plugin-proposal-decorators", { legacy: true } ],
    "babel-plugin-styled-components"
  ],
  "env": {
    "development": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-proposal-object-rest-spread",
        ["@babel/plugin-proposal-decorators", { legacy: true } ],
        "babel-plugin-styled-components"
      ]
    },
    "test": {
      "plugins": [
        "remove-decorator"
      ]
    }

包.json

  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-decorators": "7.0.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
    "@babel/polyfill": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-flow": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^21.2.0",
    "babel-loader": "^8.0.0",
    "babel-plugin-remove-decorator": "^1.0.0",
    "babel-plugin-styled-components": "^1.3.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-polyfill": "6.26.0",

最佳答案

我解决了这个问题 npm install -D @babel/plugin-proposal-decorators

"test": {
  "plugins": [["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }], "remove-decorator"]
}

将其添加到我的 babel.conf.js 文件中。

关于babeljs - Babel 7 更新搞砸了 Cannot use the decorators and decorators-legacy plugin 的 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134173/

相关文章:

javascript - 箭头函数使用 Babel 作为类属性

javascript - 脚本5022 : Exception thrown and not caught

javascript - js es6类构造函数在构造函数实例化之前运行

reactjs - 使用jest.mock时如何模拟拦截器(('axios')?

javascript - 如何为 Mobx 启用装饰器?

javascript - 标准化 TS 和 Babel 之间的默认导入的正确方法是什么?

javascript - 模仿使用 module.exports 的模块的结构

javascript - Angular Unit Test (w/Jest) 一项导入和使用 esm 模块的服务;

vue.js - VueJS - 使用 vue-test-utils 进行单元测试会出错 - TypeError : _vm. $t 不是函数

node.js - 使用 Supertest 进行测试时在 header 部分设置 Bearer Token