Reactjs Jest重用模拟下一个/路由对象
我喜欢:Reactjs Jest重用模拟下一个/路由对象,reactjs,unit-testing,jestjs,next.js,Reactjs,Unit Testing,Jestjs,Next.js,我喜欢: jest.mock('next/router', () => ({ push: jest.fn(), events: { on: jest.fn(), off: jest.fn() }, beforePopState: jest.fn(() => null) })); 我需要在很多地方使用它,这就是为什么我想重复使用它。我所尝试的, 我创建了文件夹\uuuumocks\uuuuu和文件mock.js mock.js: export defa
jest.mock('next/router', () => ({
push: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn()
},
beforePopState: jest.fn(() => null)
}));
我需要在很多地方使用它,这就是为什么我想重复使用它。我所尝试的,
我创建了文件夹\uuuumocks\uuuuu
和文件mock.js
mock.js:
export default () => jest.mock('next/router', () => ({
push: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn()
},
beforePopState: jest.fn(() => null)
}));
在我的component.test.js导入模拟中。
component.test.js:
import '../__mocks__/mocks';
但当我渲染组件时,其中有一个错误:
it('should mount and sub to "routerChangeStart"', async () => {
render(<LoseChangesWarning dirty isSubmitting={false} />);
await wait(() => {
expect(Router.events.on).toHaveBeenCalledTimes(1);
expect(Router.beforePopState).toHaveBeenCalled();
});
it('should mount and sub to“routerChangeStart”,async()=>{
render();
等待等待(()=>{
expect(Router.events.on.).tohavebeincalledtimes(1);
期望(Router.beforepostate).tohavebeincalled();
});
错误消息:
未找到路由器实例。
您应该只在应用程序的客户端中使用“下一个/路由器”