T
TanStack17mo ago
multiple-amethyst

Mock useIsFetching in Jest

Hi, I know it is recommended to not mock React Query directly in your unit tests, but mock the network calls instead. This is what I do in 99% of my tests, but there are a few cases where I do mock hooks like useIsFetching and useIsMutating directly:
import * as reactQuery from '@tanstack/react-query';

jest.spyOn(reactQuery, 'useIsMutating').mockReturnValue(1);
import * as reactQuery from '@tanstack/react-query';

jest.spyOn(reactQuery, 'useIsMutating').mockReturnValue(1);
I'm migrating to v5 and now my tests fail with the following error: TypeError: Cannot redefine property: useIsMutating. Is there a better way to do mocks like this?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?