测试失败,显示SyntaxError: Unexpected token export。

5

将“office-ui-fabric-react”软件包从“5.124.0”更新到“6.128.0”后,我的所有测试都失败了,并出现以下错误:

 FAIL  src\***.test.tsxTest suite failed to run

\node_modules\office-ui-fabric-react\lib\Callout.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './components/Callout/index';
                                                                                         ^^^^^^

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
2个回答

4
如果你正在使用 create-react-app,你可能不想弹射它。
为了在不弹射的情况下解决这个问题,我们需要能够修改 jest 配置。
幸运的是,有这个库 https://github.com/timarney/react-app-rewired
按照它的说明,在 CRA 项目中安装 react-app-rewired。
然后你需要修改你的 package.json,包括 "jest" 配置。
"jest": {
  "moduleNameMapper": {
    "office-ui-fabric-react/lib/(.*)$": "office-ui-fabric-react/lib-commonjs/$1"
  },
 "transformIgnorePatterns": [
   "node_modules/(?!office-ui-fabric-react)"
 ]
}

资源:https://github.com/OfficeDev/office-ui-fabric-react/wiki/Fabric-6-Release-Notes


更新

最新的create-react-app已经支持moduleNameMappertransformIgnorePatterns配置,不再需要使用react-app-rewired了。

https://create-react-app.dev/docs/running-tests/#configuration


在我的情况下,需要在 jest.config.js 中进行更改,而不是 package.json,就像 https://github.com/OfficeDev/office-ui-fabric-react/wiki/Fabric-6-Release-Notes 文档所述。 - stefan.seeland
如果你和我一样遇到了同样的问题,这个方法也适用于@fluentui-react。 - sigfried

0

export 用于 ES 模块,而因为 Jest 在 Node 中运行,所以需要 common JS 模块。请参阅 transformIgnorePatterns 文档,了解如何在 TypeScript 设置中将其转换为 common JS。


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