question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useRouter not working with jest

See original GitHub issue

Verify 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:open
  • Created a year ago
  • Reactions:16
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
tuckwillicommented, Sep 8, 2022

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(),
}));
1reaction
martin-fvcommented, Jul 5, 2022

Same problem here. Maybe this is the solution? https://github.com/scottrippey/next-router-mock

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found