Jest在使用Babel ^7.0.0-beta.51时的问题

6

我按照你建议的一切步骤来让 Babel 7 能够与 Jest 配合使用。我安装了 @babel/corebabel-core,但它仍然无法工作。

{
  "devDependencies": {
    "@babel/cli": "^7.0.0-beta.51",
    "@babel/core": "^7.0.0-beta.51",
    "@babel/node": "^7.0.0-beta.51",
    "@babel/plugin-transform-modules-commonjs": "^7.0.0-beta.51",
    "@babel/preset-flow": "^7.0.0-beta.51",
    "babel-core": "^7.0.0-beta.51",
    "jest-cli": "^23.1.0"
  }
}

在运行 yarn jest 命令后:
yarn run v1.9.0-20180612.1255
$ /Users/m/Desktop/k/node_modules/.bin/jest
 FAIL  packages/services/src/Client/__tests__/Client.js
  - Test suite failed to run

    Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

      at throwVersionError (node_modules/@babel/helper-plugin-utils/lib/index.js:65:11)
      at Object.assertVersion (node_modules/@babel/helper-plugin-utils/lib/index.js:13:11)
      at _default (node_modules/@babel/plugin-transform-modules-commonjs/lib/index.js:51:7)
      at node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
          at Array.map (<anonymous>)

也许对于那些在Babel 7和Jest上遇到问题的人来说,这篇React测试教程可能会很有用。 - Robin Wieruch
3个回答

4
我在Jest的GitHub问题中看到了你的问题 -- 我想那是你,但我通过查看`create-react-app`仓库并了解它如何集成Jest来解决了问题。具体来说,`packages/react-scripts/package.json`文件包含您需要的内容。 "@babel/core": "7.0.0-beta.46", "@babel/runtime": "7.0.0-beta.46", "autoprefixer": "8.5.0", + "babel-core": "7.0.0-bridge.0", "babel-eslint": "8.2.3", "babel-jest": "22.4.3", 请添加带有`+`的行,或更改您拥有的版本即可。

3
即使 Jest 页面显示它包含 babel-jest,我发现将其添加到我的项目中可以解决问题。

0

1
感谢您发布答案。如果答案本身是完整的,那么它们就是最好的。您能否添加一个解释,说明OP的问题是什么,以及您的答案如何解决这个问题? - SuperShoot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接