-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot read property 'prefetch' and 'push' of null #16864
Comments
I had the same issue. But i also had a backlog thing to update my "react-tiny-popover" (from 3 to 5). After i updated it (and fixed the breaking changes) this also seems to have been fixed. |
Sounds like the context value is not passed through correctly in the external component you're using. |
Will fix it :) |
I can't reproduce the issue. Seems to work fine https://codesandbox.io/s/cannot-read-property-prefetch-and-push-of-null-16864-5kcj3 |
I've been getting the same issue since v9.5(.0?) but with using sweetalert2-react-content v3.1.0 |
I've been getting this error in my react-testing-library tests with v9.5 where I am using in the RouterContext
I don't have the same error with 9.4.4 |
Seeing the same thing as soon as I install Next 9.5.3. For me, calling
output:
conversely, using a standard anchor tag:
works fine. I dug into the entire stack trace and can't find a single call to |
I'm facing the same issue here @mikerid94 |
I experience the same error TypeError: Cannot read property 'push' of null Not sure why though. It seems like sometimes [Edit]. I found that the link which triggers the bug, in my case, is located inside a [Edit 2] @juliosampaio @mikerid94 I tried to pass the context as you are doing, but found that error is still hapenning because the Context is not the same. So in the link when router is grabbed from
It might help some people |
@Clement-Bresson
Note: I am using |
I was able to work around the issue by mocking jest.mock('next/link', () => ({ children }) => children); |
I can confirm it worked for me as well @Clement-Bresson , thanks! |
+1, experiencing all of the above issues when combining React Testing Library and the Link component. Having to fall back on Cypress, which is suboptimal for integration tests IMO. |
My tests are failing now for more or less each It happened after an upgrade to 10.0.2 (or 10.0.3), it works fine in 10.0.1. Seems to be alot of talk about prefetching in the changelog so maybe one of those PR is to blame. To busy to dig into some reproduction at the moment and i will just skip my tests for now, and probably movre these more complicated tests to cypress instead. |
@olaj I'm seeing the same. Tests work fine in 10.0.1 but fail with this prefetch null error in 10.0.3. I'm using react-testing-library. I don't use router-context. |
This is the problematic line that my tests are complaining about: next.js/packages/next/client/link.tsx Line 49 in 397a375
That method is called from here: next.js/packages/next/client/link.tsx Line 264 in 397a375
The code was added in this PR: #18904 |
I had a different error message I managed to fix it by mocking my router like this: import React, { ReactElement } from "react";
import { render, RenderResult } from "@testing-library/react";
import { NextRouter } from "next/router";
import "@testing-library/jest-dom";
import { RouterContext } from "next/dist/next-server/lib/router-context";
export const mockRouter: NextRouter = {
basePath: "",
pathname: "/",
route: "/",
asPath: "/",
query: {},
push: jest.fn(),
replace: jest.fn(),
reload: jest.fn(),
back: jest.fn(),
prefetch: jest.fn().mockResolvedValue(undefined), // This one fixed it for me
beforePopState: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
},
isFallback: false,
};
export const renderWithRouter = (component: ReactElement): RenderResult => {
return render(
<RouterContext.Provider value={mockRouter}>
{component}
</RouterContext.Provider>
);
}; and then in my tests, I call |
The failing tests that were throwing this sort of error was pinning my project at Nextv9.4 so I made a stripped back repo to cover all the router tests I needed for Nextv10.
The router mock I ended up with was very similar to @AustinShelby's: import { RouterContext } from "next/dist/next-server/lib/router-context";
export const routerMock = {
basePath: "",
pathname: "/",
route: "/",
asPath: "/",
query: {},
push: jest.fn().mockResolvedValue(true),
replace: jest.fn().mockResolvedValue(true),
reload: jest.fn(),
back: jest.fn(),
prefetch: jest.fn().mockResolvedValue(undefined),
beforePopState: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
},
isFallback: false,
};
...
render(
<RouterContext.Provider value={routerMock}>
<Component />
</RouterContext.Provider>
);
... |
Thank you for solution. A better way of mocking jest.mock('next/link', () => {
const React = require('react');
const { UrlObject } = require('url');
type Url = string | typeof UrlObject;
type LinkProps = {
href: Url;
as?: Url;
};
return ({ children, href }: React.PropsWithChildren<LinkProps>) =>
React.cloneElement(React.Children.only(children), { href });
}); FYI as of |
Any ideas on how to solve this issue on StoryBook ?? We have created a separate issue but it issue got closed as duplicated of this issue Thank You! |
@hems updating import React from "react";
import { RouterContext } from 'next/dist/next-server/lib/router-context';
export const decorators = [
(Story) => (
<RouterContext.Provider value={{
push: () => Promise.resolve(),
replace: () => Promise.resolve(),
prefetch: () => Promise.resolve()
}}>
<Story />
</RouterContext.Provider>
),
]; |
@see vercel/next.js#16864 for more informations about next Link component breaking in a test environment
same issue as @mikerid94 in version 10.0.1 this error does not exist |
Fixes #16864 The `router` can be missing in a test environment when trying to render a `Link` component. This PR bails out of `router.prefetch()` when `router` is missing. The alternative is for users to mock `next/link` or to mock the `router` and wrap their test components. Please let me know any feedback.
If I get it right, mocking Reading code from the PR #18904 I don't get why it broke suddenly though |
@eric-burel I don't know about using Webpack aliases. What happened when it broke is that a feature was introduced that tries to prefetch any internal url as soon as In any case, I submitted a patch #19857 that was just merged this week and is included in the latest release v10.0.5 yesterday, so upgrading to that version should fix it in both tests and stories. Let me know if that is not the case. |
I was using Webpack alias to mock imports from I think the blame is not really in Next, but actually in end-user Storybook install. It's perfectly normal to have to setup a decorator to make a framework work (eg loading Material UI theme, adding i18n, loading app global context, etc. etc.)., Next is no exception. So @tiaanduplessis is not only just a quickfix but actually a decorator we may want to have in our Next/Storybook apps to mock the router in any case. |
Update for Storybook: you can now use this awesome plugin: https://storybook.js.org/addons/storybook-addon-next-router |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Cannot read property 'prefetch' and 'push' of null
This bug starts coming when I updated the next version from 9.1.4 to 9.5.2 for both production and dev build.
Describe the bug
Cannot read property 'prefetch' of null
Cannot read property 'push' of null
To Reproduce
it's coming when Link Tag is used in the Popover element (https://react-bootstrap-v3.netlify.app/components/popovers/)
Click on
Expected behavior
the error should not be there.
Screenshots
System information
Additional context
When I add
prefetch={false}
to link then the prefetch error doesn`t come while renderingbut push error is still there on click of the Link
The text was updated successfully, but these errors were encountered: