Skip to content

Instantly share code, notes, and snippets.

@Julianhm9612
Last active March 7, 2024 20:02
Show Gist options
  • Save Julianhm9612/4a25e19985b2bc877bc02188952fb8e5 to your computer and use it in GitHub Desktop.
Save Julianhm9612/4a25e19985b2bc877bc02188952fb8e5 to your computer and use it in GitHub Desktop.
Jest - Mock useMutation
// Method 1
jest.mock('react-query', () => ({
useMutation: jest.fn()
}));
const mockMutationFn = jest.fn().mockRejectedValue(new Error('Mutation failed'));
(useMutation as jest.Mock).mockReturnValue([mockMutationFn, {}]);
expect(actionCreatorsModal.setIsError).toHaveBeenCalledWith(true, expect.any(Object));
// Method 2
jest.mock('react-query', () => ({
useMutation: jest.fn()
}));
const mockMutationFn = { mutate: () => mockedMutate() };
(useMutation as jest.Mock).mockReturnValue(mockMutationFn);
expect(mockedMutate).toHaveBeenCalled();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment