Open fmacherey opened 2 years ago
Same problem here. Maybe this is the solution? https://github.com/scottrippey/next-router-mock
Hey @martin-fv yes this fixed the test for me. But i keep this issue open for now, since the bug is still there and might be fixed.
I'm encountering the same issue as well
Is there any update?
not sure if this helps anyone else but I was seeing this issue after updating when I had next/router
mocked like this:
import { useRouter } from 'next/router';
jest.mock('next/router');
changing to the following fixed the issue for me:
import { useRouter } from 'next/router';
jest.mock('next/router', () => ({
useRouter: jest.fn(),
}));
not sure if this helps anyone else but I was seeing this issue after updating when I had
next/router
mocked like this:import { useRouter } from 'next/router'; jest.mock('next/router');
changing to the following fixed the issue for me:
import { useRouter } from 'next/router'; jest.mock('next/router', () => ({ useRouter: jest.fn(), }));
@tuckwilli thank you, I've been looking for a fix all day and this works for me but can I know more, what is the difference?
For Next.js 14 you can use:
jest.mock("next/navigation", () => ({
useRouter: jest.fn(),
}));
No need to import useRouter
🙂
Verify canary release
Provide environment information
What browser are you using? (if relevant)
Chrome
How are you deploying your application? (if relevant)
No response
Describe the Bug
When updating from next@12.1.6 to 12.2.x I got the following error message in my jest tests:
With next 12.1.6 it was no problem to import useRouter and mock it with
useRouter.mockReturnValue...
but now this fails.Expected Behavior
jest tests should run as before and no warning for useRouter should be thrown.
Link to reproduction
example.com
To Reproduce
see above