React Native: Jest did not exit one second after the test run completed #1847
Send to my agentDescribe the bug When testing react native code that uses react-query you'll get the following error:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.
To Reproduce Steps to reproduce the behavior:
- Clone https://github.com/flextremedev/ReactNativePlayground
- Run "yarn"
- Run "yarn test react-query"
- See error
Expected behavior I expected the test run to complete without leaving open handles.
Yes, it's me again :)
Oh lol, sorry. I thought I checked the usernames 🙈 . I hope that someone with more knowledge (than me) of react-native and jest can tell us here how to proceed :)
The problem is setTimeout for cacheTime which is 5 minutes by default. When I set cacheTime to 0 jest stops complaining.
One solution would be to use jest.useFakeTimers() but this doesn't work with waitFor from testing-library/react-native and testing-library/hooks as of now.
jest.useFakeTimers can be used as soon as @testing-library/react-native v8.0.0 is released. It works with v8.0.0-rc.0. This is an okay solution but I would also love to be able to clear the cacheTimeout at the end of my tests but queryClient.clear doesn't work for me. Am I just using the wrong function?
Where are we on this? Are you still having issues @flextremedev >?
Yes. This won't be a much of a problem as soon as @testing-library/react-native has released v8. But it would still be nice to be able to clean up / clear the cacheTimeout in afterEach or at the end of a test. Unfortunately I couldn't get this to work as mentioned above.
Hello ! I have the same issue here on a
react-nativebased project@testing-library/react-native@7.2.0react-query@3.12.0jest@25.5.4@testing-library/jest-native@4.0.1
But setting a
cacheTimeof0in theQueryClientdid the trick :)But setting a cacheTime of 0 in the QueryClient did the trick :)
that makes sense because the timer is likely related to garbage collection, which depends on cacheTime. It can be set globally on the
QueryClientProviderthat you are using in the tests, so it won't influence production.Is that workaround, plus the proposed fix with
@testing-library/react-nativev8 (still in rc) enough to close this issue?I encountered the same issue.
It does not seem to happen when cacheTime is 0 OR Infinity.
In my tests the
afterEach(() => { queryClient.clear() });workaround solved it, but it seems ugly (have to put the client in a module top-level variable).@TkDodo I don't think setting cacheTime globally will work if you override it in specific
useQuerycalls ?TkDodo closed this
In my tests the
afterEach(() => { queryClient.clear() });workaround solved it, but it seems ugly (have to put the client in a module top-level variable).Right after creating queryClient instance before tests, I set:
queryClient.setDefaultOptions({ queries: { cacheTime: 0 } })cacheTime to 0 works for me.
Leaving a note for future me, you also need to set this for mutations if you encounter this issue with your useMutation tests! 🙈
export const createTestQueryClient = () => new QueryClient({ defaultOptions: { queries: { cacheTime: 0, retry: false, }, mutations: { cacheTime: 0, retry: false, }, }, });Hi. I get this problem, but I am not using
waitForfrom@testing-library/react-native. I am usingwatiForreturned fromrenderHookexported from@testing-library/react-hooks.I have used the config for QueryClient as explained above by tomskopek but I still have the issue:
Jest has detected the following 1 open handle potentially keeping Jest from exiting: ● MESSAGEPORT 1 | import React from "react"; > 2 | import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ^ 3 | import { renderHook, act } from "@testing-library/react-hooks"; 4 | import { resetAllWhenMocks, verifyAllWhenMocksCalled } from "jest-when";The issue goes away when I do not use the jest preset
react-native.For those using v5, you have to replace
cacheTimewithgcTime:const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: Infinity, }, mutations: { retry: false, gcTime: Infinity, }, }, });
Sign in to join this conversation.
have you seen https://github.com/tannerlinsley/react-query/discussions/1692 ?