【问题标题】:How to use babel-jest to transpile tests如何使用 babel-jest 转译测试
【发布时间】:2019-02-15 14:35:31
【问题描述】:

我正在为 React 应用程序编写测试,并在编译我在测试文件中导入的类时遇到了问题。运行测试套件时

i get the following error:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/node_modules/react-redux/es/connect/connect.js:5
import connectAdvanced from '../components/connectAdvanced';
^^^^^^ 

我尝试使用babel-jest 来使用transform 属性和modulePathIgnorePatterns 进行转译。我的 jest.config.json 看起来像:

{
  "moduleNameMapper": {
    ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy"
  },
  "setupFiles": [
    "raf/polyfill",
    "<rootDir>/test/testSetup.js"
  ],
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  }
}

我的 babel.rc 文件(在根目录下)

{
    "presets": [ "react", "es2015", "stage-2" ],
    "plugins": [
        "transform-class-properties",
        "transform-object-rest-spread",
    ]
}

我的 package.json 中有以下包:

 "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.1",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "eslint": "^5.3.0",
    "eslint-plugin-react": "^7.10.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^23.5.0",
    "babel-jest": "^23.6.0"
    "mkdirp": "^0.5.1",
    "prettier": "^1.13.7",
    "prettier-eslint": "^8.8.2",
    "prop-types": "^15.6.0",
    "react-test-renderer": "^16.5.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5",
    "webpack-merge": "^4.1.4",
    "yargs": "^12.0.1"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "babel-polyfill": "^6.26.0",
    "css-loader": "^1.0.0",
    "eventing-bus": "^1.3.3",
    "filesize": "^3.6.1",
    "i18next": "^11.5",
    "node-sass": "^4.8.3",
    "react": "^16.4",
    "react-circular-progressbar": "^1.0.0",
    "react-dom": "^16.4",
    "react-redux": "^5.0",
    "react-truncate": "^2.4.0",
    "redux": "^4.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.22.1"
  }

谁知道我如何修改我的jest.config.babelrc 文件,以便我的测试可以顺利编译?提前致谢。

【问题讨论】:

标签: reactjs babeljs babel-jest


【解决方案1】:

你的问题是:来自/node_modules的js文件默认没有编译。

在浏览器中运行代码完全没问题(这就是为什么不编译此类模块的原因,因为您不需要这样做)。

但是 jest 使用不理解 import 语法的 Node 运行。最好的解决方案是enable babel transpilation of node_modules when you run tests

{"env": {
    "development": {
        "plugins": ["transform-es2015-modules-commonjs"]
    },
    "test": {
        "plugins": ["transform-es2015-modules-commonjs"]
    }
}}

在修复此类问题时运行玩笑时也使用--no-cache

【讨论】:

  • 感谢您的回答。但是我不明白我如何才能在测试环境和开发环境之间产生差异。将链接中提供的行添加到.babelrc 并安装transform-es2015-modules-commonjs 似乎不起作用。但是,当我添加 "transformIgnorePatterns": ["/node_modules/(?!react-redux)"] 时,react-redux 的错误消失了,但会出现在 lodash-es 中的导入。你能更详细地解释一下你的意思吗?
  • 你能给我一个 lodash-es 错误信息吗?我的意思是您可以自定义 babel 行为取决于 process.env.NODE_ENV 变量。 Jest 默认设置为 'test'
  • 与上述答案中提供的错误消息相同,仅适用于 lodash-es:node_modules/lodash-es/isPlainObject.js:1 ({"Object.&lt;anonymous&gt;":function(module,exports,require,__dirname,__filename,global,jest){import baseGetTag from './_baseGetTag.js'; ^^^^^^ SyntaxError: Unexpected token import
  • 所以本质上还是关于导入:import baseGetTag from './_baseGetTag.js'; ^^^^^^ SyntaxError: Unexpected token import
  • 是的,这里也一样,您只为react-redux 启用了转换,现在它卡在lodash-es 上,将它添加到transformIgnorePatterns,这样它也可以被转换。它可能会再发生几次,但最终您将转译所有需要转译的模块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
相关资源
最近更新 更多