Skip to content

Instantly share code, notes, and snippets.

@CarmeloRicarte
Last active November 8, 2024 20:54
Show Gist options
  • Save CarmeloRicarte/ee7b9908c0ef20eae32428de77a0cd4a to your computer and use it in GitHub Desktop.
Save CarmeloRicarte/ee7b9908c0ef20eae32428de77a0cd4a to your computer and use it in GitHub Desktop.
useNavigate mock with Vitest
// this is how to mock partial library for mock a method, in this case, useNavigate
const mockedUseNavigate = vi.fn();
vi.mock("react-router-dom", async () => {
const mod = await vi.importActual<typeof import("react-router-dom")>(
"react-router-dom"
);
return {
...mod,
useNavigate: () => mockedUseNavigate,
};
});
// then in expect you can call mockedUseNavigate
// version for Jest
const mockUseNavigate = jest.fn();
const mockUseLocation = jest.fn();
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom');
return {
...originalModule,
useNavigate: () => mockUseNavigate,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment