How to test next/navigation
?
#42527
Replies: 13 comments 5 replies
-
Try this with next-router-mock. I'm using vitest, but similar thing can be done with Jest I guess.
Tests pass for me at least I'm not blocked. |
Beta Was this translation helpful? Give feedback.
-
I am trying
But getting : |
Beta Was this translation helpful? Give feedback.
-
Here's how I mocked import { useRouter } from "next-router-mock";
jest.mock("next/navigation", () => ({
useRouter,
usePathname: jest.fn().mockReturnValue('/some-route'),
})); |
Beta Was this translation helpful? Give feedback.
-
So far I've been able to get by doing this: const nextRouterMock = require('next-router-mock')
jest.mock('next/router', () => nextRouterMock)
jest.mock('next/navigation', () => {
const { useRouter } = nextRouterMock
const usePathname = () => {
const router = useRouter()
return router.pathname
}
const useSearchParams = () => {
const router = useRouter()
return new URLSearchParams(router.query)
}
return {
useRouter,
usePathname,
useSearchParams
}
}) |
Beta Was this translation helpful? Give feedback.
-
I made a import mockRouter from "next-router-mock";
import { createDynamicRouteParser } from "next-router-mock/dynamic-routes";
jest.mock("next/router", () => jest.requireActual("next-router-mock"));
mockRouter.useParser(
createDynamicRouteParser([
// @see https://github.com/scottrippey/next-router-mock#dynamic-routes
])
);
jest.mock<typeof import("next/navigation")>("next/navigation", () => {
const actual = jest.requireActual("next/navigation");
const nextRouterMock = jest.requireActual("next-router-mock");
const { useRouter } = nextRouterMock;
const usePathname = jest.fn().mockImplementation(() => {
const router = useRouter();
return router.asPath;
});
const useSearchParams = jest.fn().mockImplementation(() => {
const router = useRouter();
return new URLSearchParams(router.query);
});
return {
...actual,
useRouter: jest.fn().mockImplementation(useRouter),
usePathname,
useSearchParams,
};
});
export { mockRouter }; This is based off @kiyui 's implementation(THANK YOU!!), but expanded to have actual mock functions and add support for dynamic routes. |
Beta Was this translation helpful? Give feedback.
-
Is there any way of doing this without using the |
Beta Was this translation helpful? Give feedback.
-
Is anyone know how to do it in Next + Cypress: This is my code:
|
Beta Was this translation helpful? Give feedback.
-
Bump, seems like |
Beta Was this translation helpful? Give feedback.
-
tried to mock the useSearchParams function like this: still doesn't work, can't get it to work and it's producing error: any lead of how can test it properly? |
Beta Was this translation helpful? Give feedback.
-
namespace is not as value jest |
Beta Was this translation helpful? Give feedback.
-
In case someone is looking for a solution with next 14 🙇🏻 import { useSearchParams } from 'next/navigation';
const MyComponent = () => {
const searchParams = useSearchParams();
const status = searchParams.get('status');
// ...
} import * as mockRouter from 'next-router-mock';
const useRouter = mockRouter.useRouter;
jest.mock('next/navigation', () => ({
...mockRouter,
useSearchParams: () => {
const router = useRouter();
const path = router.query;
return new URLSearchParams(path as never);
},
}));
describe('MyComponent', () => {
it('should render with a "status" search param having the "cool" value ...', () => {
mockRouter.default.push('/?status=cool');
render(<MyComponent />);
// ...
});
}); |
Beta Was this translation helpful? Give feedback.
-
Small change to the above code that got it working for us at [email protected], [email protected] using TypeScript // __mocks__/next/navigation.ts
const nextRouterMock = jest.requireActual(
'next-router-mock'
) as typeof import('next-router-mock');
const { useRouter: useRouterMock } = nextRouterMock;
const usePathname = jest.fn().mockImplementation(() => {
const router = useRouterMock();
return router.pathname;
});
const useSearchParams = jest.fn().mockImplementation(() => {
const router = useRouterMock();
return new URLSearchParams(router.query as Record<string, string>);
});
const useRouter = jest.fn().mockImplementation(useRouterMock);
export { useRouter, usePathname, useSearchParams }; |
Beta Was this translation helpful? Give feedback.
-
I love writing tests. When I was migrating
next/router
tonext/navigation
, I found I didn't know how to write tests onnext/navigation
.next-router-mock doesn't support
next/navigation
for now. I'd like to hear how folks handle this problem.related
Beta Was this translation helpful? Give feedback.
All reactions