-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Zustand testing throws an error "TypeError: store.getState is not a function" #905
Comments
Is it possible to reproduce it with codesandbox? Hope someone can help this. Also, there are some open issues for testing that needs help. |
Sorry Not really familiar how to create components and jest test in sandbox that is why I can't reproduce it. It looks like a typescript issue. Similar issues has been submitted in zustand. https://stackoverflow.com/questions/68896812/using-a-zustand-function-mock-with-jest-results-in-typeerror-create-is-not-a |
The SO one's error is
|
Ohh nevermind I got it: To do this you should use the actual store of your app
|
Can close now?? |
I came across the same issue. I am using TypeScript and followed the setup guide. Turns out the error is caused by currying the Hence, when mocking the
@dai-shi it would be great to update the test guide. I've been banging my head for hours before I discovered the cause. |
Can you open a PR? |
@dai-shi sure I can take care of that |
Just a heads up, the currying fix on the mock A little hacky, but to support both types of export const create = <S>(createState: StateCreator<S>) => {
return typeof createState === 'function'
? createInternalFn(createState)
: createInternalFn;
};
const createInternalFn = <S>(createState: StateCreator<S>) => {
const store = actualCreate(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
}; |
@grigoll @FancyVase @dai-shi here's the PR #1739 |
@FancyVase that solutions works great when using the example here Appreciate it 🙏 |
Thanks @FancyVase Your solutions worked like a charm. I had been hitting my head for a long time as one of my tests were not passing. |
In the documentation it states that adding this in JEST would enable testing for ZUSTAND state.
But it seems to throw an error "TypeError: store.getState is not a function"
I am using typescript not sure if this causes some issues. Also, it is not well documented on how to use this test in jest. Just want to have values on my states so that they can be used on my componentes during rendering of test components.
The text was updated successfully, but these errors were encountered: