syedsibtain
Thu Apr 27 2023
So if we want to mock the useRouter hook in NextJs, we can use 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.
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.