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
I would like to get an idea how to test hooks that useSwr without deduplication: 0.
Lets say I have more than one test of a hook and between tests I use cache.clear however all other tests except the first run behave weird because the deduplication logic still kicks in despite the cache being emptied between tests.
I could wrap the tests in a global config, that sets the default deduplication time to zero but that feels wrong.
It seems using jest.useFakeTimers in a beforeAll and jest.runAllTimers() in an afterAll to fix those issues.
Hook
importuseSWRfrom"swr"typeResult=[/** Can be a string or an Error */ip: string|undefined|Error,refresh: ()=>Promise<any>,loading: boolean]/** * Hook for detecting the current public IP address. * * @returns the public IP Address or an error, a refresh function and loading indicator */exportdefaultfunctionuseInternetAddress(): Result{const{data: ip,
revalidate,
isValidating,}=useSWR<string|Error,Error>("https://ip.seeip.org",(url)=>fetch(url).then((response)=>response.text()).catch(()=>newError("Fehler"))// i81n)return[ip,revalidate,isValidating]}
Test
// Test/* eslint-disable i18next/no-literal-string */import{act,renderHook}from"@testing-library/react-hooks"importFetchMockfrom"jest-fetch-mock"import{cache}from"swr"importuseInternetAddressfrom"./useInternetAddress"FetchMock.enableMocks()beforeEach(()=>{cache.clear()// Clears the dedupe timeouts of swrjest.useFakeTimers()})afterEach(()=>{act(()=>{jest.runAllTimers()})})test("should render the hook",async()=>{const{ result, waitForNextUpdate }=renderHook(()=>useInternetAddress())expect(result.error).toBeUndefined()awaitwaitForNextUpdate()})test("should return ip",async()=>{FetchMock.mockOnce("192.168.1.666")const{ result, waitFor }=renderHook(()=>useInternetAddress())// Loadingexpect(result.current[2]).toBe(true)awaitwaitFor(()=>expect(result.current[0]).toBe("192.168.1.666"))// Not loading anymoreexpect(result.current[2]).toBe(false)})test("should refresh",async()=>{FetchMock.mockOnce("192.168.1.666")const{ result, waitFor }=renderHook(()=>useInternetAddress())awaitwaitFor(()=>expect(result.current[0]).toBe("192.168.1.666"))FetchMock.mockOnce("192.168.1.667")awaitact(()=>result.current[1]())awaitwaitFor(()=>expect(result.current[0]).toBe("192.168.1.667"))})test("IP can be error object",async()=>{FetchMock.mockRejectOnce(newError("Network error"))const{ result, waitForNextUpdate }=renderHook(()=>useInternetAddress())awaitwaitForNextUpdate()expect(result.current[0]).toEqual(newError("Fehler"))})
Without the fake timers all tests fail (with timeout) except the first one (which sets the cache for the first time).
Running each test individually each runs just fine.
Questions
Why does cache.clear not reset deduplication for all cache keys?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to get an idea how to test hooks that
useSwr
withoutdeduplication: 0
.Lets say I have more than one test of a hook and between tests I use
cache.clear
however all other tests except the first run behave weird because the deduplication logic still kicks in despite the cache being emptied between tests.I could wrap the tests in a global config, that sets the default deduplication time to zero but that feels wrong.
It seems using
jest.useFakeTimers
in abeforeAll
andjest.runAllTimers()
in anafterAll
to fix those issues.Hook
Test
Without the fake timers all tests fail (with timeout) except the first one (which sets the cache for the first time).
Running each test individually each runs just fine.
Questions
cache.clear
not reset deduplication for all cache keys?Beta Was this translation helpful? Give feedback.
All reactions