-
Notifications
You must be signed in to change notification settings - Fork 39
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
Support Next 13 'next/navigation' router #67
Comments
Where do you get this error? Storybook? Jest? |
@scottrippey I got the error in storybook and jest both once I moved to new Although I managed to fix it in jest by mocking the |
I have the same problem but without the new |
I get the same thing when trying to add MemoryRouterProvider as a storybook decorator, using nextjs 13.0.2, not using the new |
receiving an error in storybook when using the MemoryRouterProvider using nextjs 13.0.3, and NOT using the new *** update *** and I also hard code the |
Regarding Can anyone create a CodeSandbox to demonstrate this issue? Did anyone upgrade from Next 12, where it was working, and then Next 13 broke? (just to verify that it's a Next 13 issue?) |
I was able to resolve the issue @e-roy reported above in https://github.com/Soil-labs/eden-app-2-FrontEnd/pull/688#pullrequestreview-1207242476. We had to move our decorators from We also needed to change the import to |
Using |
import { addDecorator } from '@storybook/react';
This will fix it. |
So there are 2 separate issues here, hopefully this message helps people
|
I found a way to make Maybe this can help other people. vi.mock('next/router', () => require('next-router-mock'));
vi.mock('next/navigation', () => ({
...require('next-router-mock'),
useSearchParams: () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const router = require('next-router-mock').useRouter();
const path = router.asPath.split('?')?.[1] ?? '';
return new URLSearchParams(path);
},
})); |
This is a great start, thank you! |
@mxswat your solution is super helpful! I'm using
I'm still looking for a solution so that I can define the path the test should see, but very happy to be moving again! |
I also made a new version that supports import * as mockRouter from 'next-router-mock';
const useRouter = mockRouter.useRouter;
export const MockNextNavigation = {
...mockRouter,
notFound: vi.fn(),
redirect: vi.fn().mockImplementation((url: string) => {
mockRouter.memoryRouter.setCurrentUrl(url);
}),
usePathname: () => {
const router = useRouter();
return router.asPath;
},
useSearchParams: () => {
const router = useRouter();
const path = router.query;
return new URLSearchParams(path as any);
},
}; And I use it like this, import mockRouter from 'next-router-mock';
import { MockNextNavigation } from '@/test-mocks/next-navigation';
vi.mock('next/navigation', () => MockNextNavigation); And very important, reset the router URL in the beforeEach(() => {
mockRouter.setCurrentUrl('/'); |
I've started a branch to implement these |
Reopened for tracking purposes. |
How can we extend the |
@scottrippey any review here? |
PRs are welcome :) |
Must be nice to have an implementation to mock
useRouter
ofnext/navigation
The text was updated successfully, but these errors were encountered: