首页 > 解决方案 > Babel 7 更新了无法同时使用装饰器和装饰器遗留插件的玩笑测试

问题描述

我正在升级到 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 来运行我的测试,因为没有它就无法开玩笑。你认为这可能是问题所在?

  "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",

标签: babeljsjestjs

解决方案


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

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

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


推荐阅读