How to mock usePathname and useRouter from next-intl client in Jest? #331
-
Hey, I have a component which is using usePathname and useRouter from "next-intl/client". I just cannot get jest working, not sure what I am overlooking, I tried it like this: // If the tested component uses features from Next.js, you have to mock them.
// Note that next-intl only has an optional dependency on Next.js.
jest.mock('next/navigation', () => ({
useRouter() {
return {
push: () => jest.fn(),
replace: () => jest.fn(),
};
},
}));
jest.mock('next/navigation', () => ({
usePathname() {
return '';
},
}));
jest.mock('next-intl/client', () => ({
useRouter() {
return {
push: () => jest.fn(),
replace: () => jest.fn(),
};
},
}));
jest.mock('next-intl/client', () => ({
usePathname() {
return '';
},
}));
jest.mock('next/router', () => ({
useRouter() {
return {
push: () => jest.fn(),
replace: () => jest.fn(),
};
},
}));
jest.mock('next/router', () => ({
usePathname() {
return '';
},
})); But I always encounter
Any hints on what I can do? 🤔 |
Beta Was this translation helpful? Give feedback.
Answered by
chrislicodes
Jun 17, 2023
Replies: 1 comment 2 replies
-
Ah nevermind, they just have to go into the same mock call 🫡 jest.mock('next-intl/client', () => ({
useRouter() {
return {
push: () => jest.fn(),
replace: () => jest.fn(),
};
},
usePathname() {
return '';
},
})); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
chrislicodes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah nevermind, they just have to go into the same mock call 🫡