jest/prefer-to-have-been-called-times
Require toHaveBeenCalledTimes() instead of checking the length of mock.calls
fixable
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 shows the expected and received number of calls.
--fix rewrites the assertion.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/prefer-to-have-been-called-times" = "error" # or "warn"Examples#
example.ts1 problem
import { expect, jest, test } from "@jest/globals";
test("saves each item once", () => {
const save = jest.fn();
saveAll(["a", "b"], save);
expect(save.mock.calls).toHaveLength(2)Prefer toHaveBeenCalledTimes() over toHaveLength() when asserting mock call counts;
});example.tsNo problems
import { expect, jest, test } from "@jest/globals";
test("saves each item once", () => {
const save = jest.fn();
saveAll(["a", "b"], save);
expect(save).toHaveBeenCalledTimes(2);
expect(save.mock.calls[0]).toHaveLength(1); // the arguments of the first call
});