# bun:test/prefer-to-have-been-called-times

> Require `toHaveBeenCalledTimes()` instead of checking the length of `mock.calls`

When `expect(save.mock.calls).toHaveLength(2)` fails, the message is about the length of an array. `expect(save).toHaveBeenCalledTimes(2)` says what the test means, and its failure message gives the expected and the received number of calls.

`--fix` rewrites the assertion.

{/* 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-to-have-been-called-times" = "error" # or "warn"
```

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

## Examples

Reported:

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

test("saves each item once", () => {
  const save = mock();
  saveAll(["a", "b"], save);
  expect(save.mock.calls).toHaveLength(2);
});
```

Not reported:

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

test("saves each item once", () => {
  const save = mock();
  saveAll(["a", "b"], save);
  expect(save).toHaveBeenCalledTimes(2);
  expect(save.mock.calls[0]).toHaveLength(1); // the arguments of the first call
});
```
