close

prefer-called-with

Configuration

rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/prefer-called-with': 'error',
    },
  },
]);

Rule Details

The toHaveBeenCalled() and toBeCalled() matchers assert that a mock function has been called one or more times, without checking the arguments passed. The assertion is stronger when arguments are also validated using toHaveBeenCalledWith() or toBeCalledWith(). When some arguments are difficult to check, using generic matchers such as expect.anything() at least enforces the number and position of arguments.

Examples of incorrect code for this rule:

expect(someFunction).toBeCalled();

expect(someFunction).toHaveBeenCalled();

Examples of correct code for this rule:

expect(noArgsFunction).toHaveBeenCalledWith();

expect(roughArgsFunction).toHaveBeenCalledWith(
  expect.anything(),
  expect.any(Date),
);

expect(anyArgsFunction).toHaveBeenCalledTimes(1);

expect(uncalledFunction).not.toHaveBeenCalled();

Original Documentation