title: Mock redux saga middleware with Jest
body: <p>I've got this configureStore.js which configures my enhanced redux store and persists it to the localStorage:</p>
  
  <pre class="lang-js prettyprint-override"><code>// @flow
  
  import 'babel-polyfill'
  
  import { addFormSubmitSagaTo } from 'redux-form-submit-saga/es/immutable'
  import { applyMiddleware, createStore, compose } from 'redux'
  import { autoRehydrate, persistStore } from 'redux-persist-immutable'
  import { browserHistory } from 'react-router'
  import { combineReducers } from 'redux-immutable'
  import { fromJS } from 'immutable'
  import { routerMiddleware } from 'react-router-redux'
  import createSagaMiddleware from 'redux-saga'
  
  import rootReducer from './reducers'
  import sagas from './sagas'
  
  export default function configureStore () {
    const initialState = fromJS({ initial: { dummy: true } })
    const sagaMiddleware = createSagaMiddleware()
    const middleware = [ routerMiddleware(browserHistory), sagaMiddleware ]
  
    const enhancer = compose(
      autoRehydrate(),
      applyMiddleware(...middleware),
      window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ &amp;&amp;
        window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__()
    )
    const store = createStore(
      combineReducers(rootReducer),
      initialState,
      enhancer
    )
    // Persist store to the local storage
    persistStore(store, { blacklist: [ 'form', 'routing' ] })
    // Decorate with Redux Form Submit Saga
    // and create hook for saga's
    const rootSaga = addFormSubmitSagaTo(sagas)
    sagaMiddleware.run(rootSaga)
  
    return store
  }
  </code></pre>
  
  <p>Now I'm trying to test this file using Jest:</p>
  
  <pre class="lang-js prettyprint-override"><code>import configureStore from './../configureStore'
  
  import * as reduxPersistImmutable from 'redux-persist-immutable'
  import * as redux from 'redux'
  import createSagaMiddleware from 'redux-saga'
  
  describe('configureStore', () =&gt; {
    it('persists the store to the localStorage', () =&gt; {
      reduxPersistImmutable.persistStore = jest.fn()
      redux.createStore = jest.fn()
      createSagaMiddleware.default = jest.fn(() =&gt; {
        run: () =&gt; jest.fn()
      })
  
      configureStore()
    })
  })
  </code></pre>
  
  <p>Everything runs smooth in this test until configureStore reaches the <code>sagaMiddleware.run(rootSaga)</code> line. It throws the following error:</p>
  
  <pre><code>    FAIL  app/__tests__/configureStore.js
   ● Console
  
     console.error ../node_modules/redux-saga/lib/internal/utils.js:206
       uncaught at check Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware
  
   ● configureStore › persists the store to the localStorage
  
     Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware
  
       at check (../node_modules/redux-saga/lib/internal/utils.js:50:11)
       at Function.sagaMiddleware.run (../node_modules/redux-saga/lib/internal/middleware.js:87:22)
       at configureStore (app/configureStore.js:38:18)
       at Object.&lt;anonymous&gt; (app/__tests__/configureStore.js:17:60)
       at process._tickCallback (../internal/process/next_tick.js:103:7)
  </code></pre>
  
  <p>This error indicates that the mock function does not work as I intend: it doesn't seem to be overwritten and is called from within redux-saga. Mocking redux.createStore works just fine.</p>