Jestjs 测试失败,SyntaxError:意外的令牌导出
将包“office ui fabric react”从“5.124.0”更新为“6.128.0”后,我的所有测试都失败,出现以下错误:Jestjs 测试失败,SyntaxError:意外的令牌导出,jestjs,Jestjs,将包“office ui fabric react”从“5.124.0”更新为“6.128.0”后,我的所有测试都失败,出现以下错误: FAIL src\***.test.tsx ● Test suite failed to run \node_modules\office-ui-fabric-react\lib\Callout.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__f
FAIL src\***.test.tsx
● Test 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)
FAIL src\***.test.tsx
● 测试套件无法运行
\节点\u模块\office ui fabric react\lib\Callout.js:1
({“Object.”:函数(模块、导出、require、u dirname、u filename、全局、jest){export*from./components/Callout/index';
^^^^^^
SyntaxError:意外的令牌导出
在ScriptTransformer.\u transformAndBuildScript(节点\u模块/jest运行时/build/ScriptTransformer.js:289:17)
导出
用于ES模块,但由于Jest在节点中运行,它需要通用JS模块。请参阅有关如何使用TypeScript设置将其转换为通用JS的文档。如果您使用的是create react app,您可能不想将其弹出
为了在不弹出的情况下解决这个问题,我们需要能够在不弹出的情况下修改jest配置
幸运的是有这个图书馆
按照其说明,在您的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)"
]
}
资源:
更新 最新的create react应用程序已经支持
moduleNameMapper
和transformIgnorePatterns
配置。
因此,不再需要使用重新布线的react应用程序
在我的例子中,更改需要在jest.config.js而不是package.json中完成,就像文档一样。