由于结构中的某些问题,单元测试用例在我的 React 应用程序中失败
Unit test case is failing in my React app because of some issue in fabric
我是 React 新手,也想使用 Office React ui 来满足我们的一项要求ui我关注 git 中心并且能够使用 Office ui 在我的组件中反应组件,但是 运行 我的第一个测试用例 App.js 它给了我以下错误。
E:\net_react\my-new-app\ClientApp\node_modules\office-ui-fabric-react\lib\Fabric.js:1
({"Object.":function(module,exports,require,__dirname,__filename,global,jest){export * from './components/Fabric/index';
^^^^^^
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object. (src/components/Login.js:13:592)
at Object. (src/components/Home.js:2:14)
我的 Login.js
中有一个导入语句
import { Fabric } from '../../node_modules/office-ui-fabric-react/lib/Fabric';
错误是因为您的测试工具不支持 ES 6 模块(这是 Fabric 6 中的 lib 中的模块)。
尝试从 office-ui-fabric-react/lib-commonjs/Fabric
或 office-ui-fabric-react
导入(这会影响包大小,除非您能够使用 Tree Shaking)或修改测试工具的模块映射以重定向 lib/
导入 lib-commonjs
。
更新
为了详细说明上面的答案,Fabric release notes 有 Jest 配置指南:
moduleNameMapper: {
"office-ui-fabric-react/lib/(.*)$": "office-ui-fabric-react/lib-commonjs/"
}
我是 React 新手,也想使用 Office React ui 来满足我们的一项要求ui我关注 git 中心并且能够使用 Office ui 在我的组件中反应组件,但是 运行 我的第一个测试用例 App.js 它给了我以下错误。
E:\net_react\my-new-app\ClientApp\node_modules\office-ui-fabric-react\lib\Fabric.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){export * from './components/Fabric/index'; ^^^^^^
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17) at Object. (src/components/Login.js:13:592) at Object. (src/components/Home.js:2:14)
我的 Login.js
中有一个导入语句import { Fabric } from '../../node_modules/office-ui-fabric-react/lib/Fabric';
错误是因为您的测试工具不支持 ES 6 模块(这是 Fabric 6 中的 lib 中的模块)。
尝试从 office-ui-fabric-react/lib-commonjs/Fabric
或 office-ui-fabric-react
导入(这会影响包大小,除非您能够使用 Tree Shaking)或修改测试工具的模块映射以重定向 lib/
导入 lib-commonjs
。
更新
为了详细说明上面的答案,Fabric release notes 有 Jest 配置指南:
moduleNameMapper: {
"office-ui-fabric-react/lib/(.*)$": "office-ui-fabric-react/lib-commonjs/"
}