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 switched some of my code over to using the global mutate function and found that it worked fine in my application, but failed in my unit tests: Performing the mutation failed to trigger re-render in components that consumed the relevant data. I eventually tracked down the difference in behavior to the custom cache provider I was using, as advised in the docs (Reset Cache Between Test Cases).
Expected Behavior
I'd expected the mutate function imported directly from swr to have the same behavior as
In implementation terms, this difference makes sense: The custom cache provider is scoped to components rendered under that <SWRConfig>, and the global mutate function can't be scoped in that way. But as a user, it's very surprising that the global mutate function would stop working in my unit tests because I'm following the recommended steps for resetting the cache.
Some possible solutions, in order of preference:
Provide a more direct way of resetting the cache than using a custom provider, e.g. a global resetCache function that would clear the default provider cache. I could call it in a beforeEach when running my tests.
Update the docs for Reset Cache Between Test Cases to warn about this side effect.
Additional Context
swr v2.2.2
The text was updated successfully, but these errors were encountered:
Bug report
The
mutate
function fromis a no-op when a custom cache provider is used.
Description / Observed Behavior
I switched some of my code over to using the global
mutate
function and found that it worked fine in my application, but failed in my unit tests: Performing the mutation failed to trigger re-render in components that consumed the relevant data. I eventually tracked down the difference in behavior to the custom cache provider I was using, as advised in the docs (Reset Cache Between Test Cases).Expected Behavior
I'd expected the
mutate
function imported directly fromswr
to have the same behavior assince this difference isn't noted in the Global Mutate docs.
In implementation terms, this difference makes sense: The custom cache provider is scoped to components rendered under that
<SWRConfig>
, and the globalmutate
function can't be scoped in that way. But as a user, it's very surprising that the globalmutate
function would stop working in my unit tests because I'm following the recommended steps for resetting the cache.Some possible solutions, in order of preference:
provider
, e.g. a globalresetCache
function that would clear the default provider cache. I could call it in abeforeEach
when running my tests.Additional Context
swr v2.2.2
The text was updated successfully, but these errors were encountered: