Vitest and Zustand only works with slight modifications from docs #1918
-
So I spent a large amount of time writing the initial tests, having a really hard time understanding how I am supposed to make the tests work, and when I did get it to work in the end, it was only because I managed to find some other github repositories using Vitest and Zustand, which was slightly modified from the docs. Its a long post due to the examples, so TLDR:
To this:
A bit unsure why, but I added all my details below if its interesting. First here is my implementation:
Now I started running some tests, and initially I got this error:
The component I was testing also has a
Okay then, I will try to mock the function that the useEffect triggers, as I don't really need it to run, then the test runs further, and now it complains that my store is empty:
As you can see from step 1, I do initialize it with a value (and works fine outside of tests), and then step 2 and 3 is how I import and use it. However, now At this point I starting giving up, and started browsing github for other projects that uses Vitest and Zustand, and I noticed one that simply did not use the mock of zustand in Changing |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 19 replies
-
@P1llus sorry, the latest version of testing docs is not released yet but you can check it out directly here https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.md
|
Beta Was this translation helpful? Give feedback.
-
@P1llus Hello there! Today, I encountered a similar issue, and it turned out that I didn't have the
I believe it would be helpful to mention that the |
Beta Was this translation helpful? Give feedback.
@P1llus sorry, the latest version of testing docs is not released yet but you can check it out directly here https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.md