尝试运行Jest时无法识别Babel插件

时间:2018-11-07 16:46:07

标签: javascript webpack jestjs babel

我刚刚开始编写一些Jest测试,但立即遇到了“未知插件”错误,否则似乎是可以正常工作的Webpack / Babel设置,并且在npm run dev / {{ 1}}阶段。

具体地说,我得到npm run build

(错误的显示方式与我在Windows上的Git Bash中一样。)

我肯定已经安装了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"

我的@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

关于可能出什么问题的任何想法吗?

1 个答案:

答案 0 :(得分:2)

尝试运行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