Last active
March 7, 2024 20:02
-
-
Save Julianhm9612/4a25e19985b2bc877bc02188952fb8e5 to your computer and use it in GitHub Desktop.
Jest - Mock useMutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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