- Published
- Author
- Syed SibtainSystem Analyst
So if we want to mock the useRouter hook in NextJs, we can use
The "vi" library's function vi.mock allows us to simulate the behaviour of the useRouter hook. The mock function returns an object that mimics the Router object's properties and methods.
If we do not use it, the test might fail and we get the following error.
vi for it.The "vi" library's function vi.mock allows us to simulate the behaviour of the useRouter hook. The mock function returns an object that mimics the Router object's properties and methods.
Code
vi.mock("next/router", () => ({
useRouter: () => ({
replace: vi.fn(),
}),
}));replace: a mock function that simulates the behavior of the replace method of the Router object.If we do not use it, the test might fail and we get the following error.
Error: NextRouter was not mounted.