我的应用程序有一个auth微前端,它创建了一个用于与应用程序中的所有微前端共享auth状态的单例实例。
auth类公开了一些方法,如
{
getUser: () => {...}
isAuthenticated: () => true
...
}在我加载到MF中的应用程序中,获取实例,并调用我需要的方法。
import { Auth } from 'auth/services/Auth';
const auth = Auth.getInstance();
const component = () => {
const isAuthenticated = auth.isAuthenticated();
...
}我使用webpack模块联邦在运行时加载MF。
plugins: [
new ModuleFederationPlugin({
name: 'mf',
remotes: {
auth: 'auth@https://localhost:3099/remoteEntry.js',
...
},
}),
],有什么办法可以在Cypress中嘲弄auth实例吗?我想将使用模拟数据公开的方法称为auth实例。
发布于 2022-10-31 11:18:25
不是为某些工具做广告,但为此,我将使用Mockoon记录和模拟身份验证部分。
发布于 2022-11-01 00:39:57
模拟应用程序内部的一种方法是公开一个供测试修改的引用。
import { Auth } from 'auth/services/Auth';
const auth = Auth.getInstance();
if (window.Cypress) {
window.auth
}
const component = () => {
const isAuthenticated = auth.isAuthenticated();
...
}测试
cy.visit('/', {
onBeforeLoad: (win) => {
cy.stub(win.auth, 'isAuthenticated').returns(true)
},
})https://stackoverflow.com/questions/74112677
复制相似问题