jest/no-alias-methods
Disallow matcher aliases such as toBeCalled in favor of toHaveBeenCalled
fixable
Some matchers have two names. For example, toBeCalled is the same matcher as toHaveBeenCalled. If a project uses both, searching for one name misses the tests that use the other. Jest 30 removed the aliases, so a test that uses one fails after the upgrade.
--fix renames the matcher.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/no-alias-methods" = "error" # or "warn"Examples#
example.ts1 problem
import { expect, jest, test } from "@jest/globals";
test("saves the draft", () => {
const save = jest.fn();
save("draft");
expect(save).toBeCalledWithUnexpected alias toBeCalledWith("draft");
});example.tsNo problems
import { expect, jest, test } from "@jest/globals";
test("saves the draft", () => {
const save = jest.fn();
save("draft");
expect(save).toHaveBeenCalledWith("draft");
});The full list:
| Reported | Write instead |
|---|---|
toBeCalled | toHaveBeenCalled |
toBeCalledTimes | toHaveBeenCalledTimes |
toBeCalledWith | toHaveBeenCalledWith |
lastCalledWith | toHaveBeenLastCalledWith |
nthCalledWith | toHaveBeenNthCalledWith |
toReturn | toHaveReturned |
toReturnTimes | toHaveReturnedTimes |
toReturnWith | toHaveReturnedWith |
lastReturnedWith | toHaveLastReturnedWith |
nthReturnedWith | toHaveNthReturnedWith |
toThrowError | toThrow |