Zustand + jest #1960
Unanswered
AlaaInflyter
asked this question in
General
Zustand + jest
#1960
Replies: 1 comment 6 replies
-
That's why you need to mock zustand in order to implement a logic to reset stores afterEach test |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys !
First of all, thanks for the work here !
I've setup a react-native stack with zustand in it. And wrote some tests for it.
As per the tutorial, i have setup everything regarding tests.
The tutorials show a "store" file with a classic store hook exported from it.
The issue here is with parallel tests.
If a component imports a hook from a store.ts file, and you test that component in different tests, these tests are gonna use the same store instance, which might cause conflicts and unwanted rerendering. Basically some kind of flakiness.
Any ideas to go around this ?
I've tried mocking like below, using a function that generates a store using initial state:
The mocking works for the initial state (so at least I can set whatever state i want), but the parallel issue remains: the test cases in the same file are using the same store instance.
Another possible thing is to use the context API, and wrap each unit test with a context provider that injects a new store. But what i like most about zustand is that its very lightweight, no boilerplate. So it would be a bit sad to go through providers :/
Beta Was this translation helpful? Give feedback.
All reactions