Skip to content

Files

Latest commit

 

History

History
22 lines (16 loc) · 606 Bytes

prefer-called-with.md

File metadata and controls

22 lines (16 loc) · 606 Bytes

Pattern: Generic mock call assertion

Issue: -

Description

Using toBeCalled() or toHaveBeenCalled() without specifying arguments makes test intentions unclear. Use toBeCalledWith() or toHaveBeenCalledWith() to explicitly verify the arguments passed to mocks.

Examples

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);