question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Zustand testing throws an error "TypeError: store.getState is not a function"

See original GitHub issue

In the documentation it states that adding this in JEST would enable testing for ZUSTAND state.

const create = createState => {
  const store = actualCreate(createState);
  const initialState = store.getState();
  storeResetFns.add(() => store.setState(initialState, true));
  return store;
};

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Boniqxcommented, Apr 12, 2022

Ohh nevermind I got it:

To do this you should use the actual store of your app


    const initialStoreState = useStore.getState()
     
     beforeEach(() => {
        useStore.setState(initialStoreState, true)
      })

    useStore.setState({ me: memberMockData, isAdmin: true })
0reactions
dai-shicommented, Apr 12, 2022

Can close now??

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - _this.store.getState is not a function when testing ...
This error means that store can't get the state correctly. I would recommend mocking the store using redux-mock-store and import ...
Read more >
React Testing Library and the “not wrapped in act” Errors
I recently upgraded React and React testing library. Very happy about the upgrade. But I start to see test errors like this: In...
Read more >
TypeScript Guide - Zustand Documentation
Zustand is a small, fast and scalable bearbones state-management solution, it has a comfy api based on hooks.
Read more >
zustand - Bountysource
TypeError : store.getState is not a function import { act } from 'react-dom/test-utils'; import actualCreate from 'zustand'; // a variable to hold reset ......
Read more >
React - How to fix this.setState is not a function error
This error happens when JavaScript can't find the setState() function from the context of this keyword. There are two ways to fix this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found