bun:test/prefer-called-once
Require toHaveBeenCalledOnce() instead of toHaveBeenCalledTimes(1)
fixable
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#
example.ts1 problem
import { expect, mock, test } from "bun:test";
test("notifies the listener", () => {
const listener = mock();
publish("ready", listener);
expect(listener).toHaveBeenCalledTimes(1)The use of toBeCalledTimes(1) and toHaveBeenCalledTimes(1) is discouraged.;
});example.tsNo problems
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.