# prefer-called-with

Rule `vitest/prefer-called-with`. Enforce using `toBeCalledWith()` or `toHaveBeenCalledWith()`

- 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: #1687 by reach

## Linters

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

## Code examples

### Incorrect: the rule reports this

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

### Correct: the rule accepts this

```ts
test('foo', () => {
  const mock = vi.fn()
  mock('foo')
  expect(mock).toBeCalledWith('foo')
  expect(mock).toHaveBeenCalledWith('foo')
  expect(mock).toHaveBeenCalledExactlyOnceWith('foo')
})
```

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-with": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

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

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useCalledWith": "error"
      }
    }
  }
}
```

## Related rules

- [jest/prefer-called-with](https://lintradar.dev/rules/jest/prefer-called-with.md): Suggest using `toHaveBeenCalledWith()`
- [vitest/prefer-called-times](https://lintradar.dev/rules/vitest/prefer-called-times.md): Enforce using `toBeCalledTimes(1)` or `toHaveBeenCalledTimes(1)`
- [vitest/prefer-describe-function-title](https://lintradar.dev/rules/vitest/prefer-describe-function-title.md): Enforce using a function as a describe title over an equivalent string
- [vitest/no-test-return-statement](https://lintradar.dev/rules/vitest/no-test-return-statement.md): Disallow return statements in tests
- [vitest/prefer-equality-matcher](https://lintradar.dev/rules/vitest/prefer-equality-matcher.md): Enforce using the built-in equality matchers
- [vitest/no-hooks](https://lintradar.dev/rules/vitest/no-hooks.md): Disallow setup and teardown hooks

---

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