Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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().

Configure

Off by default. To turn it on:

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

Examples

Reported:

import { expect, mock, test } from "bun:test";

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

Not reported:

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.