gpt4 book ai didi

jestjs - Babel7 Jest 意外 token 导出

转载 作者:行者123 更新时间:2023-12-04 22:09:41 24 4
gpt4 key购买 nike

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

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

我的 Jest 配置
{
"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 并导出一个对象。

所以给你一些步骤:

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

    关于jestjs - Babel7 Jest 意外 token 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52387820/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com