Pattern: Generic mock call assertion
Issue: -
Using toBeCalled()
or toHaveBeenCalled()
without specifying arguments makes test intentions unclear. Use toBeCalledWith()
or toHaveBeenCalledWith()
to explicitly verify the arguments passed to mocks.
Example of incorrect code:
expect(mockFunction).toBeCalled();
expect(mockFunction).toHaveBeenCalled();
Example of correct code:
expect(mockFunction).toBeCalledWith(expectedArg);
expect(mockFunction).toBeCalledWith(expect.any(String));
expect(mockFunction).toBeCalledTimes(1);