Skip to content
← Back to rules

vitest/prefer-called-exactly-once-with Style

⚠️ 🛠️ A dangerous auto-fix is available for this rule.

What it does ​

It checks when a target is asserted with both toHaveBeenCalledOnce and toHaveBeenCalledWith instead of toHaveBeenCalledExactlyOnceWith.

Why is this bad? ​

The reader must deduce from both expectations that the spy function is called once and with specific arguments.

Examples ​

Examples of incorrect code for this rule:

js
test("foo", () => {
  const mock = vi.fn();
  mock("foo");
  expect(mock).toHaveBeenCalledOnce();
  expect(mock).toHaveBeenCalledWith("foo");
});

Examples of correct code for this rule:

js
test("foo", () => {
  const mock = vi.fn();
  mock("foo");
  expect(mock).toHaveBeenCalledExactlyOnceWith("foo");
});

How to use ​

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

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-called-exactly-once-with": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/prefer-called-exactly-once-with": "error",
  },
});
bash
oxlint --deny vitest/prefer-called-exactly-once-with --vitest-plugin

Version ​

This rule was added in v1.58.0.

References ​