Jest: Assertion Enhancement: toBeCalled(x)

Created on 14 Sep 2016  路  1Comment  路  Source: facebook/jest

I'd like to request that we either add toBeCalledOnce() or toBeCalled(x) where x represents the number of times the mock function was called.

There is a subtle but significant difference between these two assertions:

expect(foo).toBeCalled()
expect(foo.mock.calls.length).toBe(1)

The difference is that the second asserts that it was called only one time. No more, no less. The first only asserts that it was called, not that it was only called one time.

Most helpful comment

actually.. we already have expect(jest.fn()).toHaveBeenCalledTimes(x)
but unfortunately it's not documented :)
sorry.. we're still cleaning things up in our documentation

>All comments

actually.. we already have expect(jest.fn()).toHaveBeenCalledTimes(x)
but unfortunately it's not documented :)
sorry.. we're still cleaning things up in our documentation

Was this page helpful?
0 / 5 - 0 ratings