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!
source share