Shall I use toHaveBeenCalledTimes in my tests to count how many times dispatch was called?

Viewed 552

I often see my teammates use toHaveBeenCalledTimes in their tests with Jest to count how many times the dispatch method of Redux store was called.

It seems useless and redundant for me: if we check that the proper actions were called, why do we need to count the number of calls for the dispatch method?

Looks like this:

expect(setEnabled).toHaveBeenCalled(); expect(updateUserData).toHaveBeenCalled(); expect(dispatch).toHaveBeenCalledTimes(2);

Does it really make sense to check the number of calls for dispatch method? Looks like that in such case we don't really test that the function works as appropriate, but test the implementation.

1 Answers

In rare cases, nested or circular dispatch calls can cause unexpected state behavior, but in most cases, toHaveBeenCalledTimes() check here is reduntant, if no complex Redux middleware is implemented. However, redundant testing is arguably better than insufficient testing, so it cannot be necessarily concluded that your teammates are doing something wrong.

Related