# bun:test/prefer-called-once

> Require `toHaveBeenCalledOnce()` instead of `toHaveBeenCalledTimes(1)`

`toHaveBeenCalledTimes(1)` and `toHaveBeenCalledOnce()` assert the same thing. This rule picks the shorter one, so the whole project writes it the same way. The alias `toBeCalledTimes(1)` is reported too.

`--fix` rewrites both to `toHaveBeenCalledOnce()`, because `bun:test` has no `toBeCalledOnce()`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"bun:test/prefer-called-once" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
import { expect, mock, test } from "bun:test";

test("notifies the listener", () => {
  const listener = mock();
  publish("ready", listener);
  expect(listener).toHaveBeenCalledTimes(1);
});
```

Not reported:

{/* good */}
```ts
import { expect, mock, test } from "bun:test";

test("notifies the listener", () => {
  const listener = mock();
  publish("ready", listener);
  expect(listener).toHaveBeenCalledOnce();

  publish("done", listener);
  expect(listener).toHaveBeenCalledTimes(2);
});
```

## When not to use it

`bun:test/prefer-called-times` asks for the opposite. Turn on one of the two, not both.
