Babel7 Jest 意外的令牌导出

Babel7 Jest unexpected token export

我在使用 Babel7 的项目中 运行ning 开玩笑测试时遇到问题。用于与 babel6 完美转换的测试。它还可以通过 Babel7 与 webpack 完美编译,但由于转译错误而无法 运行 开玩笑地进行测试。我究竟做错了什么?

react/node_modules/generic-redux-root/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './source/CreateReduxRoot';
                                                                                         ^^^^^^

SyntaxError: Unexpected token export

我的笑话配置

{
"unmockedModulePathPatterns": [
  "<rootDir>/node_modules/react",
  "<rootDir>/node_modules/react-dom",
  "<rootDir>/node_modules/react-addons-test-utils",
  "<rootDir>/node_modules/fbjs",
  "enzyme"
],
"roots": [
  "<rootDir>/__tests__"
],
"transformIgnorePatterns": [
  "node_modules/(^generic-)/i", //a module matching this is throwing an error
  "node_modules/react-infinite-scroller"
],
"setupFiles": [
  "./jestsetup.js"
],
"snapshotSerializers": [
  "enzyme-to-json/serializer"
],
"testResultsProcessor": "./jestTrxProcessor",
"verbose": true

}

我的.babelrc

{
"presets": [
    [
        "@babel/preset-env",
        {
            "targets": {
                "ie": 11
            },
            "useBuiltIns": "usage"
        }
    ],
    "@babel/preset-react"
],
"plugins": [
    "@babel/plugin-transform-runtime",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-json-strings",
    [
        "@babel/plugin-proposal-decorators",
        {
            "legacy": true
        }
    ],
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-object-assign"
]

}

我做错了什么?

这是因为 Babel 7 不再自动加载您的 .babelrc。 有一个新概念 root config,它应该位于项目的根目录中,文件必须命名为 babel.config.js 并导出一个对象。

所以给你一些要遵循的步骤:

  1. .babelrc 重命名为 babel.config.js 并确保使用 module.exports = {...}
  2. 运行 jest --clearCache 清除 Jest 内部缓存(这让我花了几个小时撞墙)
  3. 此时你的 babel 配置应该被 Jest 正确加载