Skip to content

vitest/prefer-called-times Style

What it does

This rule aims to enforce the use of toBeCalledTimes(1) or toHaveBeenCalledTimes(1) over toBeCalledOnce() or toHaveBeenCalledOnce().

Why is this bad?

This rule aims to enforce the use of toBeCalledTimes(1) or toHaveBeenCalledTimes(1) over toBeCalledOnce() or toHaveBeenCalledOnce().

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

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-times": "error"
  }
}
bash
oxlint --deny vitest/prefer-called-times --vitest-plugin

References