New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reset zustand state between tests #242
Comments
I haven't tried, but does it work? const savedState = store.getState()
store.setState(savedState, true /* replace */) |
Ah there is a Maybe the usage with testing can be added to the readme. I'm using this for now: const initialStoreState = useStore.getState();
describe('MyComponent', () => {
beforeEach(() => {
useStore.setState(initialStoreState, true);
});
... |
I actually found a pretty nice automatic way of performing store cleanups in a very transparent way (at least in React) without any change in your source or test code. I've mocked import actualCreate from 'zustand';
const stores = new Set<Function>();
const create: typeof actualCreate = createState => {
const store = actualCreate(createState);
const initialState = store.getState();
stores.add(() => store.setState(initialState, true));
return store;
};
afterEach(() => {
stores.forEach(resetFn => resetFn());
});
export default create; The idea is to mock the store creation in order keep a list of all the stores that have been created within the app, get their first/original state and then use it reset them in a jest @dai-shi I'd be willing to clean it up a bit & make it part of |
@3nvi
I think 2 or 3 seem good for now. Or, won't you create a dedicated issue to discuss and get feedback with others? |
Sure thing. I can put up a PR for the readme & create an issue illustrating a potential approach for P.S. Regarding (2), I can't seem to find a dedicated docs page and the wiki seems empty. Am I missing something? |
Sounds great.
That's correct. This would be the first one. The readme is now getting long and it would be a good chance. |
@dai-shi Does this violate the rules of hooks? You are using the useStore in the test, and its outside of a component or another custom hook. However, the rule does no seem to throw any other errors. use using the useStore allowed in tests and other non custom hook helper functions? It seems here, this is not the useStore zustand hook, but just the store returned from the 'create' function. Is that returned store still a hook? Thank you! |
👉 #1986 |
I like how simple zustand is, however I need to be able to run tests in isolation. Currently tests will fail because the state of the previous test is affecting the next test.
A solution would be a possibility to reset all stores to the initial state or a way to use a Provider to get a new instance of Zustand for every test.
I tried resetting modules in Jest, but that doesn't play well with testing-library. Currently I just added a reset action to my store.
Or is there a better way to test?
I'm using React, Jest and react-testing-library
The text was updated successfully, but these errors were encountered: