【问题标题】:Babel plugin not being recognized when trying to run Jest尝试运行 Jest 时无法识别 Babel 插件
【发布时间】:2018-11-08 01:32:12
【问题描述】:

我刚开始编写一些 Jest 测试,但在原本看起来完全正常的 Webpack/Babel 设置中立即遇到了“未知插件”错误,该设置在 npm run dev/npm run build 阶段运行良好.

具体来说,我收到的是ReferenceError: Unknown plugin "@babel/transform-async-to-generator" specified in "C:\\Users\\scott\\path\\to\\ThisProject\\.babelrc" at 0, attempted to resolve relative to "C:\\Users\\scott\\path\\to\\ThisProject"

(当我在 Windows 上的 Git Bash 中时,错误会这样读取。)

我肯定安装了@babel/plugin-transform-async-to-generator

我的package.json 的相关部分看起来像:

"scripts": {
  "test": "jest",
  "build": "webpack --mode=production",
  "dev": "webpack --mode=development"
},
"jest": {
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  }
},
"dependencies": {
  "@babel/core": "^7.1.2",
  "@babel/plugin-transform-arrow-functions": "^7.0.0",
  "@babel/plugin-transform-async-to-generator": "^7.1.0",
  "@babel/plugin-transform-modules-commonjs": "^7.1.0",
  "@babel/plugin-transform-runtime": "^7.1.0",
  "@babel/polyfill": "^7.0.0",
  "@babel/preset-env": "^7.1.0",
  "babel-loader": "^8.0.4",
  "clean-webpack-plugin": "^0.1.19",
  "copy-webpack-plugin": "^4.5.2",
  "webpack": "^4.20.2",
  "webpack-cli": "^3.1.2"
},
"devDependencies": {
  "ajv": "^6.5.4",
  "babel-jest": "^23.6.0",
  "eslint": "^5.8.0",
  "jest": "^23.6.0",
  "jsdom": "^13.0.0",
}

我的.babelrc很简单:

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "ie": "11"
                },
                "useBuiltIns": "entry"
            }
        ]
    ],
    "plugins": [
        "@babel/transform-async-to-generator",
        "@babel/transform-arrow-functions",
        "@babel/transform-modules-commonjs"
    ],
    "env": {
        "development": {},
        "test": {},
        "production": {}
    }
}

同样jest.config.js 直接来自jest --init

module.exports = {
    clearMocks: true,
    coverageDirectory: "coverage",
    testEnvironment: "jsdom"
};

对可能出现的问题有什么想法吗?

【问题讨论】:

    标签: javascript webpack jestjs babeljs


    【解决方案1】:

    尝试运行npm install --save-dev babel-jest babel-core@^7.0.0-bridge @babel/core,我相信 babel 团队发布了一个桥接包来帮助受 v7 升级影响的依赖项。

    查看这里了解更多信息:https://github.com/facebook/jest/tree/master/packages/babel-jest#usage

    【讨论】:

    • 男孩你好!!!这就是解决方案。有一个微弱的机会我自己可能会偶然发现它,但不是在一天中的这个时候经过几个小时的挣扎。非常感谢!
    猜你喜欢
    • 2015-07-31
    • 2020-09-22
    • 1970-01-01
    • 2018-11-17
    • 2017-07-27
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多