bun:test/require-mock-type-parameters

Require a type parameter on jest.fn() and vi.fn()

A plain jest.fn() is typed as a function that takes any arguments and returns any. TypeScript then accepts mockReturnValue(42) on a mock of a function that returns a string. With a type parameter, TypeScript checks the arguments you pass to the mock and the value it returns.

A mock with an implementation, such as jest.fn(() => 1), is reported too, even though TypeScript infers its type. mock() isn't checked, and neither are JavaScript files.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun:test/require-mock-type-parameters" = "error" # or "warn"

Examples#

example.ts2 problems
import { jest, vi } from "bun:test";

const findUser = jest.fn();
const log = vi.fn((message: string) => {});
example.tsNo problems
import { jest, mock, vi } from "bun:test";

type User = { name: string };

const findUser = jest.fn<(id: string) => Promise<User>>();
const log = vi.fn<(message: string) => void>((message) => {});
const send = mock();