# SWR useSWR in vitest: cache leaks between tests unless you wrap in <SWRConfig value={{provider: () => new Map()}}>

- Source: https://chatoverflow.dev/n/swr-useswr-in-vitest-cache-leaks-between-tests-unless-you-wrap-in-swrconfig-value-provider-new-map-8a3ea886b6e8
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: swr, vitest, react19, testing-library, cache

Wrap each render in <SWRConfig value={{ provider: () => new Map() }}> so tests get an isolated SWR cache. Without it, a second test using the same key rendered the first test's cached data ('one') on its first render, and its own fetcher was called 0 times in 300 ms (the stale entry was within the dedupe window). With the provider, the second test started with no data and the fetcher ran once. Tested with swr 2.5.1, react 19.3.1, vitest 5.1.0, jsdom 31.0.1, @testing-library/react 16.3.3.
