You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running the unit test for the useMuation hook,
The mocked mutation function is expected to be throw error and onError callback is executed, but it appears to not be called at all.
Your minimal, reproducible example
VScode
Steps to reproduce
Steps to Reproduce
Run the unit test for useMutation
Observe the test results.
import{mount}from'enzyme';import{act}from'react-dom/test-utils';import{useMutation}from'react-query';// Mock the useMutation hook from react-queryjest.mock('react-query');// Custom hook that uses a mutation from react-queryconstuseCustomHook=()=>{constmutation=useMutation((data)=>{mutationFn: ()=>api.post(/* data */),onError: (err)=>{toast({title: 'error',message: err.message,});},});returnmutation;};describe('useCustomHook',()=>{constmockMutation=jest.fn();beforeEach(()=>{// Mock the return value of useMutation(useMutationasjest.Mock).mockReturnValue({mutate: mockMutation});});afterEach(()=>{jest.clearAllMocks();});it('should handle error when mutation fails',async()=>{constTestComponent=()=>{useCustomHook();returnnull;// render nothing};mount(<TestComponent/>);consterror=newError('Mutation failed');mockMutation.mockImplementation(()=>{// or mockRejecteValueOncethrowerror;});awaitact(async()=>{try{awaitmockMutation(data);}catch(err){// Handle error}expect(mockError).toHaveBeenCalledWith({title: 'error',message: error.message,});});});
Expected behavior
The mockError function should be called with the following arguments:
Node.js version: 18.18.2
NPM/Yarn version: 9.8.1
React version: 18
Testing library: Jest with Enzyme
Operating System: Ubuntu
Tanstack Query adapter
react-query
TanStack Query version
5.14.2
TypeScript version
5.3.3
Additional context
No response
The text was updated successfully, but these errors were encountered:
ducle-infotrack
changed the title
Unit test for useMutation hook can't be mocked
Unit test for useMutation hook 's can't be mocked to run onError callback
Jan 11, 2024
Describe the bug
Description
When running the unit test for the
useMuation
hook,The
mocked mutation
function is expected to be throw error andonError
callback is executed, but it appears to not be called at all.Your minimal, reproducible example
VScode
Steps to reproduce
Steps to Reproduce
Run the unit test for
useMutation
Observe the test results.
Expected behavior
The
mockError
function should be called with the following arguments:How often does this bug happen?
Screenshots or Videos
No response
Platform
Node.js version: 18.18.2
NPM/Yarn version: 9.8.1
React version: 18
Testing library: Jest with Enzyme
Operating System: Ubuntu
Tanstack Query adapter
react-query
TanStack Query version
5.14.2
TypeScript version
5.3.3
Additional context
No response
The text was updated successfully, but these errors were encountered: