Accepted answer

what i essentially did was bring in my redux store (and provider) and wrapped it in a utility component as follows:

export const customprovider = ({ children }) => {
  return (
    <provider store={store}>

then, i mount the samplecomponent and run tests against it:

it('contains <childcomponent/> component', () => {
  const wrapper = mount(
      <samplecomponent {...defaultprops} />


in an attempt to make the use of decorator syntax more testable i made this:


export class anyoldclass {
  method() {


export class anyoldclass {
  method() {

export class __undecorated__anyoldclass {
  method() {

hopefully this can provide a solid option 3!


you can use name export to solve this problem:

you should have:

class samplecomponent extends react.component{

export default connect(mapstatetoprops, mapdispatchtoprops)(samplecomponent)

you can add a export before class:

export class samplecomponent extends react.component{

and import this component with no redux store:

import { samplecomponent } from 'your-path/samplecomponent';

with this solution you don't need to import store to your test files.


option 1)

you can wrap the container component with react-redux's provider component within your test. so with this approach, you actually reference the store, pass it to the provider, and compose your component under test inside. the advantage of this approach is you can actually create a custom store for the test. this approach is useful if you want to test the redux-related portions of your component.

option 2)

maybe you don't care about testing the redux-related pieces. if you're merely interested in testing the component's rendering and local state-related behaviors, you can simply add a named export for the unconnected plain version of your component. and just to clarify when you add the "export" keyword to your class basically you are saying that now the class could be imported in 2 ways either with curly braces {} or not. example:

export class mycomponent extends react.component{ render(){ ... }}


export default connect(mapstatetoprops, mapdispatchtoprops)(mycomponent)

later on your test file:

import mycomponent from 'your-path/mycomponent'; // it needs a store because you use "default export" with connect
import {mycomponent} from 'your-path/mycomponent'; // don't need store because you use "export" on top of your class.

i hope helps anyone out there.


there is also the option to use redux-mock-store.

a mock store for testing redux async action creators and middleware. the mock store will create an array of dispatched actions which serve as an action log for tests.

the mock store provides the necessary methods on the store object which are required for redux. you can specify optional middlewares and your app specific initial state.

import configurestore from 'redux-mock-store'

const middlewares = []
const mockstore = configurestore(middlewares)

const initialstate = {}
const store = mockstore(initialstate)

const wrapper = mount(<samplecomponent store={store}/>)


enzyme's mount takes optional parameters. the two that are necessary for what you need are

options.context: (object [optional]): context to be passed into the component

options.childcontexttypes: (object [optional]): merged contexttypes for all children of the wrapper you would mount samplecomponent with an options object like so:

const store = { 
  subscribe: () => {},
  dispatch: () => {},
  getstate: () => ({ ... whatever state you need to pass in ... })
const options = {
  context: { store }, 
  childcontexttypes: { store: react.proptypes.object.isrequired } 

const _wrapper = mount(<samplecomponent {...defaultprops} />, options)

now your samplecomponent will pass the context you provided down to the connected component.

Related Query

More Query from same tag