# prefer-called-once

Rule `vitest/prefer-called-once`. Enforce using `toBeCalledOnce()` or `toHaveBeenCalledOnce()`

- Source: vitest
- Technologies: Testing
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #1791 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `vitest/prefer-called-once` | no | safe | package `@vitest/eslint-plugin` | [docs](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/prefer-called-once.md) |
| Oxlint | `vitest/prefer-called-once` | no | safe | plugin `vitest`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/vitest/prefer-called-once.html) |

## Code examples

### Incorrect: the rule reports this

```ts
test('foo', () => {
  const mock = vi.fn()
  mock('foo')
  expect(mock).toBeCalledTimes(1)
  expect(mock).toHaveBeenCalledTimes(1)
})
```

### Correct: the rule accepts this

```ts
test('foo', () => {
  const mock = vi.fn()
  mock('foo')
  expect(mock).toBeCalledOnce()
  expect(mock).toHaveBeenCalledOnce()
})
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import vitest from "@vitest/eslint-plugin";

export default [
  {
    plugins: { vitest },
    rules: {
      "vitest/prefer-called-once": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "vitest"
  ],
  "rules": {
    "vitest/prefer-called-once": "error"
  }
}
```

## Related rules

- [vitest/padding-around-test-blocks](https://lintradar.dev/rules/vitest/padding-around-test-blocks.md): Enforce padding around `test` blocks
- [vitest/prefer-comparison-matcher](https://lintradar.dev/rules/vitest/prefer-comparison-matcher.md): Enforce using the built-in comparison matchers
- [vitest/padding-around-expect-groups](https://lintradar.dev/rules/vitest/padding-around-expect-groups.md): Enforce padding around `expect` groups
- [vitest/prefer-each](https://lintradar.dev/rules/vitest/prefer-each.md): Enforce using `each` rather than manual loops
- [vitest/padding-around-describe-blocks](https://lintradar.dev/rules/vitest/padding-around-describe-blocks.md): Enforce padding around `describe` blocks
- [vitest/prefer-expect-assertions](https://lintradar.dev/rules/vitest/prefer-expect-assertions.md): Enforce using expect assertions instead of callbacks

---

Web page: https://lintradar.dev/rules/vitest/prefer-called-once. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
