Skip to content

jest/prefer-called-with Style

What it does

Suggest using toBeCalledWith() or toHaveBeenCalledWith()

Why is this bad?

When testing function calls, it's often more valuable to assert both that a function was called AND what arguments it was called with. Using toBeCalled() or toHaveBeenCalled() only verifies the function was invoked, but doesn't validate the arguments, potentially missing bugs where functions are called with incorrect parameters.

Examples

Examples of incorrect code for this rule:

javascript
expect(someFunction).toBeCalled();
expect(someFunction).toHaveBeenCalled();

Examples of correct code for this rule:

javascript
expect(noArgsFunction).toBeCalledWith();
expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date));
expect(anyArgsFunction).toBeCalledTimes(1);
expect(uncalledFunction).not.toBeCalled();

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny jest/prefer-called-with --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-called-with": "error"
  }
}

References

Released under the MIT License.