jest/no-untyped-mock-factory
Require a type parameter on jest.mock() and jest.doMock() with a factory
fixable
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#
example.ts1 problem
import { jest } from "@jest/globals";
jest.mockjest.mock() factories should not be used without an explicit type parameter.("./mailer", () => ({
send: jest.fn(),
}));example.tsNo problems
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");