close

prefer-to-have-been-called

Configuration

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

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

Rule Details

In order to have a better failure message, toHaveBeenCalled() and not.toHaveBeenCalled() should be used when asserting that a mock has or has not been called.

This rule triggers a warning if toHaveBeenCalledTimes or toBeCalledTimes is used to assert that a mock has or has not been called zero times.

Examples of incorrect code for this rule:

expect(method).toHaveBeenCalledTimes(0);

expect(method).not.toHaveBeenCalledTimes(0);

Examples of correct code for this rule:

expect(method).not.toHaveBeenCalled();

expect(method).toHaveBeenCalled();

Original Documentation