useRouter not working with jest
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
[fmacherey@macbook application (feature/WR-333-update-packages *>)]$ npx --no-install next info
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64
Binaries:
Node: 16.13.1
npm: 8.1.2
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 12.2.1-canary.1
eslint-config-next: 12.2.0
react: 17.0.2
react-dom: 17.0.2
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:
FAIL components[...]/index.test.jsx
● Test suite failed to run
No router instance found.
You should only use "next/router" on the client side of your app.
> 1 | import { useRouter } from "next/router";
| ^
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
Issue Analytics
- State:
- Created a year ago
- Reactions:16
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to mock useRouter? · Issue #7479 · vercel/next.js - GitHub
I needed something that worked both in Storybook and in Jest. ... It's not ideal because I don't love overriding Router.router constantly.
Read more >Mocking NextJS router events with Jest - Stack Overflow
Based on that, I was able to put together the working solution below: jest.mock('next/router', () => ({ useRouter() { ...
Read more >How to mock Next router with Jest - DEV Community
I'm using a default static site generation (SSG) Next build. This causes a little problem, namely the query element on the router will...
Read more >Read more - back - GitHub Pages
The problem. If you happen to have Next.js useRouter hook in one of you're component to test, ... SomeComponent.test.js ... const useRouter =...
Read more >next-router-mock - npm
A fully working Jest example. import singletonRouter, { useRouter } from 'next/router'; import NextLink from 'next/link'; import { render, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
not sure if this helps anyone else but I was seeing this issue after updating when I had
next/router
mocked like this:changing to the following fixed the issue for me:
Same problem here. Maybe this is the solution? https://github.com/scottrippey/next-router-mock