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.

Issue with next/link in Next 12.2.0

See original GitHub issue

Hello!

I’ve noticed that after upgrade to newest version of Next 12.2.0 (released 3 days ago) there is an issue with tests utilising Next Link.

import Link from "next/link"
...
jest.mock("next/router", () => require("next-router-mock"));
jest.mock("next/dist/client/router", () => require("next-router-mock"));
...
it("", () => {
  const { getByText } = render(<div><Link href={"/foo"}>Foo</Link></div>);
  const foo = getByText("Foo");
  fireEvent.click(foo);
});
...

test fails with following error:

TypeError: Cannot read properties of null (reading ‘push’) 57 | fireEvent.click(foo);

After Next downgrade to previous stable version (12.1.6) everything works fine.

I am on the latest version of next-router-mock

I suspected that there may be some changes in the paths or refactor of Link component in the 12.2.0 release but didn’t found anything like that in the PR. Do you have any clue what’s going on?

Best!

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

14reactions
Damo1155commented, Jul 13, 2022

Thanks for the input.

Just to note, the above code resolves most of the issue but it still seems like you need both of the following:

jest.mock("next/dist/client/router", () => require("next-router-mock"));

jest.mock("next/dist/shared/lib/router-context", () => {
  const { createContext } = require("react");
  const router = require("next-router-mock").default;
  const RouterContext = createContext(router);
  return { RouterContext };
});

Not having line one continues to break the tests but keeping line one passes them all.

4reactions
scottrippeycommented, Sep 28, 2022

I’ve started work on this router-context concept, but it’s not quite complete, and I’m not sure if it’s a long-term solution. I’ll post here as I update.

I’m attempting to get in contact with Vercel directly, so that I can make some headway on a long-term solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Next.js 12.2
Next.js 12.2 introduces stable Middleware and On-Demand ISR, experimental Edge SSR and API Routes, and more!
Read more >
NextJS v12.2.0 causing Router null reference issues when ...
It's a problem within next-router-mock; this package has broken due to significant changes in how routing is managed in NextJS.
Read more >
Report Trouble
Having trouble? Want to report a service issue? Nextlink makes it easy to raise a support ticket and get the help you need....
Read more >
next - NPM Package Overview
Start using Socket to analyze next and its 19 dependencies to secure your app from supply chain ... [Doc] Update inline documentation for...
Read more >
Next.js 12.2: Middleware, On-Demand ISR, Experimental ...
The problem with Next.js is the examples are soooooo base that they never give a clear use-case or problem domains all these new...
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