node.jsreduxmocha.jsrxjs5redux-observable

Remove/disconnect epics from redux-observable


We run our integration tests within Node, using enzyme's mount and jsdom to render the full app. For each test, we create a new store

return createStore(
    reducer,
    stateShape,
    composeEnhancers(
        applyMiddleware(epicMiddleware, navMiddleware)
    )
);

Unfortunately, doing this for multiple tests I've noticed that each epic has been attached multiple times, so instead of one epic acting one an action 10 of the same one are! Is there a way I can run cleanup at the end of each test so that the epics are detached and no longer listening?

We're using redux, redux-observable, and enyzme+mocha to test. Thanks!


Solution

  • Every time you create a new store, you'll need to create a new instance of the epicMiddleware that you give it.

    Alternatively, there is epicMiddleware.replaceEpic(rootEpic) which lets you replace the currently running root epic, but I'm not sure that will solve your problem in this case.