Linter · Rules
jest/prefer-mock-promise-shorthand
Require mockResolvedValue() and mockRejectedValue() for mocks that return a promise
mockReturnValue(Promise.resolve(user)) and mockImplementation(() => Promise.resolve(user)) do the same thing as mockResolvedValue(user) with more code. The same goes for Promise.reject() and mockRejectedValue(), and for the Once version of each method.
--fix rewrites the call.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/prefer-mock-promise-shorthand" = "error" # or "warn"
Examples
Reported:
import { jest } from "@jest/globals";
const fetchUser = jest.fn().mockReturnValue(Promise.resolve({ name: "Ada" }));
const saveUser = jest.fn().mockImplementationOnce(() => Promise.reject(new Error("offline")));
Not reported:
import { jest } from "@jest/globals";
const fetchUser = jest.fn().mockResolvedValue({ name: "Ada" });
const saveUser = jest.fn().mockRejectedValueOnce(new Error("offline"));
An implementation that takes parameters is allowed, because its result can depend on them:
import { jest } from "@jest/globals";
const fetchUser = jest.fn().mockImplementation((id: number) => Promise.resolve({ id }));