Redux: Spying actions in unit tests

Created on 6 Oct 2015  路  2Comments  路  Source: reduxjs/redux

I have a test case where I'd like to count how many times an action is called. To do this, I'm using a spy:

import * as actions from "./actions"; // actions = { myAction: () => (/* etc */) }
import expect from "expect";

const spy = expect.spyOn(actions, "myAction");

doStuffDispatchingMyAction()
expect(spy.calls.length).toEqual(1); // throws!

Redux throws an error: Actions must be plain objects. Use custom middleware for async actions. Is this case related to this issue? How could I spy an action?

question

Most helpful comment

My guess is that you want expect.spyOn(...).andCallThrough().
Otherwise you're dispatching undefined.

All 2 comments

My guess is that you want expect.spyOn(...).andCallThrough().
Otherwise you're dispatching undefined.

Awesome thanks!

Was this page helpful?
0 / 5 - 0 ratings