Skip to content
Source
Sign inSign up
TanStack/query

React Native: Jest did not exit one second after the test run completed #1847

Send to my agent
Closedflextremedev(GitHub profile, opens in a new tab) opened this issue 14 comments

Describe 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:

  1. Clone https://github.com/flextremedev/ReactNativePlayground
  2. Run "yarn"
  3. Run "yarn test react-query"
  4. See error

Expected behavior I expected the test run to complete without leaving open handles.

  1. 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 :)

  2. 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.

  3. 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?

  4. Where are we on this? Are you still having issues @flextremedev >?

  5. 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.

  6. Hello ! I have the same issue here on a react-native based project

    • @testing-library/react-native@7.2.0
    • react-query@3.12.0
    • jest@25.5.4
    • @testing-library/jest-native@4.0.1

    But setting a cacheTime of 0 in the QueryClient did the trick :)

  7. 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 QueryClientProvider that you are using in the tests, so it won't influence production.

    Is that workaround, plus the proposed fix with @testing-library/react-native v8 (still in rc) enough to close this issue?

  8. 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 useQuery calls ?

  9. TkDodo closed this

  10. 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 } })

  11. 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,
          },
        },
      });
  12. Hi. I get this problem, but I am not using waitFor from @testing-library/react-native. I am using watiFor returned from renderHook exported 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.

  13. For those using v5, you have to replace cacheTime with gcTime:

      const queryClient = new QueryClient({
        defaultOptions: {
          queries: {
            retry: false,
            gcTime: Infinity,
          },
          mutations: {
            retry: false,
            gcTime: Infinity,
          },
        },
      });

Sign in to join this conversation.