swr icon indicating copy to clipboard operation
swr copied to clipboard

When using renderHook loadingTimeout has to be set to 0 to prevent open handles

Open pke opened this issue 2 years ago • 0 comments

Bug report

I had several react-native tests reporting "open handles" which prevented jest from finishing.

Jest has detected the following 1 open handle potentially keeping Jest from exiting: timeout

Description / Observed Behavior

Turns out its code related to "slow loading", which spawns a timer that is not properly terminated. This could be a problem of the timer implementation for react-native or related to swr, don't know.

 > 32 |   const { result, waitForNextUpdate } = renderHook(() => useVersionCheck())
         |                                         ^
      33 |   expect(result.error).toBeUndefined()
      34 |   expect(result.current.ignore).toBeUndefined()
      35 |   expect(result.current.action).toBeFalsy()

      at node_modules/swr/dist/index.js:739:29

I am now using this wrapper for all my hook related tests. If that is the expected behaviour of swr in a react-native environment, then maybe we should add this to the testing guide as a hint and maybe even export a testing helper from the package?

// Render all in a wrapper with no caching between tests
const wrapper = (props: any) => (
  <SWRConfig value={{ provider: () => new Map(), loadingTimeout: 0, dedupingInterval: 0 }} {...props} />
)

Expected Behavior

I expected to don't need to setup a test wrapper for swr.

Additional Context

SWR version: 1.2.1

pke avatar Feb 19 '22 22:02 pke