Jest mock redux-store getState () used in a module with a reduction poppy store

So, my redux store implementation might not be the best, and my knowledge of Jest is minimal.

I have three files that consist of ...

  • Module
  • React Component
  • (normal) store exported by default

My test file looks like this:

// file.test.js jest.mock( 'store', () => jest.fn() ) // external import React from 'react' import configureStore from 'redux-mock-store' // internal import Component from 'components/Component' import store from 'store' describe( 'Test My Component', () => { const mockStore = configureStore() it ( 'should equal true', () => { const initialState = { active: true } store.mockImplementation(() => mockStore( initialState )) expect( store.getState().active ).toBe( true ) } ) } ); 

The reason I'm mocking is because inside <Component /> I use a module that imports the same store and contains several functions that use store.getState() ...

So this returns

TypeError: _store2.default.getState is not a function

I went around this by making fun of a module call (which is used by <Component /> ), but I feel that I should be able to do this because I have different initial states that I want to try and not all depend on the module.

This is my first post on SO let me know. If I clarify anything!

+5
source share
1 answer

Do not import storage. We want to make fun of the store:

const store = mockStore(initialState);

0
source

Source: https://habr.com/ru/post/1268785/


All Articles