For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/jest/prefer-to-have-been-called.md.
close

prefer-to-have-been-called

Added in v0.6.0

Configuration

PresetConfigured Value
✅ jestPlugin.configs.all"error"
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