Babel7 Jest意外令牌导出

时间:2018-09-18 13:34:17

标签: jestjs babel babel-jest

使用Babel7在我的项目中运行笑话测试时遇到问题。可以与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"
]

}

我在做什么错了?

1 个答案:

答案 0 :(得分:11)

之所以发生这种情况,是因为Babel 7不再自动加载您的.babelrcroot config有一个新概念,它应该位于项目的根目录中,并且文件必须命名为babel.config.js并导出对象。

因此,您可以按照以下步骤操作:

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