jest/prefer-jest-mocked
Require jest.mocked(fn) instead of fn as jest.Mock
fixable
fetchUser as jest.Mock replaces the function's type with a mock of anything, so mockReturnValue accepts a value that the real function can never return. jest.mocked(fetchUser) keeps the parameters and the return type, so a mock value with the wrong shape is a type error.
The rule reports a cast to jest.Mock, jest.MockedFunction, jest.MockedClass or jest.MockedObject, with or without type arguments.
--fix rewrites the cast to jest.mocked().
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/prefer-jest-mocked" = "error" # or "warn"Examples#
example.ts2 problems
import { jest } from "@jest/globals";
import { fetchUser, saveUser } from "./api";
jest.mock("./api");
(fetchUser as jest.MockPrefer jest.mocked() over fn as jest.Mock.).mockReturnValue({ name: "Ada" });
const mockSaveUser = saveUser as jest.MockedFunction<typeof saveUser>Prefer jest.mocked() over fn as jest.Mock.;example.tsNo problems
import { jest } from "@jest/globals";
import { fetchUser, saveUser } from "./api";
jest.mock("./api");
jest.mocked(fetchUser).mockReturnValue({ name: "Ada" });
const mockSaveUser = jest.mocked(saveUser);