Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 在主机应用程序中模拟jest的联邦模块_Unit Testing_Webpack_Jestjs_Mocking_Webpack Module Federation - Fatal编程技术网

Unit testing 在主机应用程序中模拟jest的联邦模块

Unit testing 在主机应用程序中模拟jest的联邦模块,unit-testing,webpack,jestjs,mocking,webpack-module-federation,Unit Testing,Webpack,Jestjs,Mocking,Webpack Module Federation,事实上,问题在这里完全相同,但有不同的背景: 我是一个项目的一部分,在这个项目中,新模块联合会是从webpack使用的。基本上,我有一个主机应用程序,它使用远程应用程序。我在这里为路由做同样的事情: 我的主机应用程序导入远程应用程序的路径类似 (我从模块联盟repo中获取了这个示例:) //app1/src/App.js 从“React”导入React; 从“react router dom”导入{BrowserRouter,Route,Switch}; 从“/routes”导入本地路由;

事实上,问题在这里完全相同,但有不同的背景:

我是一个项目的一部分,在这个项目中,新模块联合会是从webpack使用的。基本上,我有一个主机应用程序,它使用远程应用程序。我在这里为路由做同样的事情:

我的主机应用程序导入远程应用程序的路径类似 (我从模块联盟repo中获取了这个示例:)

//app1/src/App.js
从“React”导入React;
从“react router dom”导入{BrowserRouter,Route,Switch};
从“/routes”导入本地路由;
从“app2/routes”导入remoteRoutes;
常量路由=[…本地路由,…远程路由];
常量应用=()=>(
应用程序1
{routes.map((route)=>(
))}
);
该组件的测试文件如下所示:

//app1/src/App.test.js
从“React”导入React;
从'react router dom'导入{MemoryRouter};
从'@testing library/react'导入{render};
从“./App”导入应用程序;
笑话模拟(“app2/routes”);
描述('App',()=>{
测试('应该呈现导航',异步()=>{
const{findByTestId}=render(
)


笑话模拟(“app2/routes”,
() => { 
//对我的远程应用程序路由的一些嘲弄
},
{virtual:true}
);
在创建
模拟工厂
之后,只需传递
{virtual:true}
选项并停止抱怨模块的存在


希望它能帮助解决同样问题的人。

作为一个总体策略。我在我的
tests
文件夹中创建了
remote-app
文件夹,并将mock放入其中,然后用文件夹按远程应用的名称进行组织:
tests>remote-app>remote-app-a.js
将这些mock文件夹放入我的
jest.config.js中e> 作为
setupfileafterenv
如下
setupfileafterenv:['@testing library/jest dom/extend expect','./tests/remote-apps/remote-app-A.js']