Skip to content
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

TypeError: Cannot read property 'getState' of undefined (Testing with Jest & Enzyme) #438

Closed
szmarci opened this issue Feb 20, 2020 · 5 comments

Comments

@szmarci
Copy link

szmarci commented Feb 20, 2020

I'm trying to test a component with Jest and Enzyme, like this:

it('active tab is first', () => {
  const store = createStore(storeModel);

  const App = () =>
    <StoreProvider store={store}>
      <Tab children="test" id="a" index={1}/>
    </StoreProvider>
  
  const wrapper = shallow(<App />);
}

I'm trying to mock out the store with an initial state, then supplying it to StoreProvider.
Tab component is hooking into the state like this:

export const Tab: FunctionComponent<IChild> = ({ children, id }) => {
  const activeId = useStoreState(state => state.activeTab);
  ....
}

The error is thrown at the useStoreState line:

TypeError: Cannot read property 'getState' of undefined

at useStoreState (node_modules/easy-peasy/src/hooks.js:38:43)
at Tab (src/components/Tab.tsx:9:20)
at ReactShallowRenderer.render (node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:858:32)
at fn (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:645:55)

Is this something I am missing or this is not supposed to work like that?

@ctrlplusb
Copy link
Owner

Hey @szmarci

Sorry, I use @testing-library/react these days so I am no longer the best to advise on this. One thought though; if you switch to Enzyme's "mount" API does it work? If so it may be an issue with React's context and Enzyme's shallow renderer.

@szmarci
Copy link
Author

szmarci commented Feb 27, 2020

Hey @ctrlplusb
Thanks for stopping by. It does work with mount, however rendering the whole tree in every test is pretty slow unfortunately

@ctrlplusb
Copy link
Owner

You are likely facing this restriction:

enzymejs/enzyme#2176

@barakadanny
Copy link

Hi @szmarci,
Did you solve the issue? or find any solution that I could use to solve the issue?

@mohamedsanoob
Copy link

mohamedsanoob commented Jan 7, 2024

I have similar issue, that I converted all .ts file to .js file in my project (Type script to java script), and while unit testing getting error as " cannot read property getState of undefined", While wrapping the testing component with "Provider store={store}"

Does anybody know the issue..?
Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants