vercel / swr

React Hooks for Data Fetching
https://swr.vercel.app
MIT License
30.24k stars 1.21k forks source link

SWR is not eventually consistent in tests #1891

Open james-reed-toshi opened 2 years ago

james-reed-toshi commented 2 years ago

Bug report

Description / Observed Behavior

I have a simple SWR/msw/react/jest setup where in one test, I have instructed MSW to return with one value from an endpoint, and in the other test, to return with a different value. SWR does not respect the new value returned from the endpoint mocked by MSW, and my test fails.

Expected Behavior

I would expect SWR, with caching, to first return the cached result, but then to return the new result given that it's different, meaning that both tests would eventually pass. I have added long timeouts and sleeps to ensure that it isn't just a timing issue on these tests.

Repro Steps / Code Example

swr_caching_react_tests.zip To run, just download, run yarn then yarn test. All versions of libs used are in package.json

Additional Context

I understand that SWR puts forward the idea of wrapping these tests with a config that has an empty provider - however, I don't see why SWR is not eventually seeing the new value coming forth in the MSW handler for the second test.

embeddedt commented 2 years ago

Hi @james-reed-toshi, try wrapping the components that you render in:

<SWRConfig value={{provider: () => new Map(), dedupingInterval: 0}}>
   <YourComponent/>
</SWRConfig>

This will disable all caching and deduping for those components and fixed the issue for me.

james-reed-toshi commented 2 years ago

@embeddedt As mentioned, I'm aware that is an option, but it still doesn't explain why SWR doesn't eventually return me the new return value for the endpoint.