Linter · Rules
jest/no-untyped-mock-factory
Require a type parameter on jest.mock() and jest.doMock() with a factory
TypeScript doesn't compare the object a jest.mock() factory returns with the module it replaces. If you rename an export or change its arguments, the mock keeps the old shape and the test still compiles. With a type parameter, jest.mock<typeof import("./mailer")>(..), the factory must return what the module exports.
The rule only runs in TypeScript files. A factory with a return type is accepted too.
--fix adds <typeof import('..')> if the module name is a string literal.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/no-untyped-mock-factory" = "error" # or "warn"
Examples
Reported:
import { jest } from "@jest/globals";
jest.mock("./mailer", () => ({
send: jest.fn(),
}));
Not reported:
import { jest } from "@jest/globals";
jest.mock<typeof import("./mailer")>("./mailer", () => ({
send: jest.fn(),
}));
jest.doMock("./clock", (): typeof import("./clock") => ({
now: () => 0,
}));
jest.mock("./database");