test-mock-untyped
Typing a mock function and the wrapper that forwards to it.
The rule
Agents read this text as written when they write and review code.
Type every jest.fn() to the real function's signature; read its source first. For an async function, include the Promise. In a jest.mock() factory, type the wrapper's parameters as the real function's, and forward them all:
const mockGetProfile = jest.fn<(params: { userId: string }) => Profile | null>();
jest.mock('@/common/utils/getProfile', () => ({
getProfile: (params: { userId: string }) => mockGetProfile(params),
}));
Never write (...args: unknown[]) in a wrapper, which fails to compile (TS2556), even in a file that already does. Never write () => mockGetProfile() for a function that takes arguments: the spy records calls with none, and toHaveBeenCalledWith fails on a call that was correct.
A stub for a library's heavily generic result type, such as a query or mutation result, is exempt: stub only the fields the unit reads, and cast loosely, as Record<string, unknown>, or as unknown as the full type where it is demanded. These rules pin your contracts, not the library's, and copying its generics adds noise, not safety.
Examples
The check flags the incorrect code and passes the correct code.
Incorrect
import { expect, describe, test, jest } from '@jest/globals';// Incorrect: no generic, so the mock accepts and returns anything.const mockGetLocale = jest.fn();const mockSaveOrder = jest.fn<(params: { id: string }) => string>();// Incorrect: the wrapper is typed to discard what the real function takes.jest.mock('@/orders/saveOrder', () => ({saveOrder: (...args: unknown[]) => mockSaveOrder(args[0] as { id: string }),}));describe('subject', () => {test('reads the locale and saves the order', () => {mockGetLocale.mockReturnValue('en-GB');mockSaveOrder.mockReturnValue('ok');expect(mockGetLocale() + mockSaveOrder({ id: 'a' })).toBe('en-GBok');});});
Correct
import { expect, describe, test, jest } from '@jest/globals';const mockGetLocale = jest.fn<() => string>();const mockSaveOrder = jest.fn<(params: { id: string }) => string>();// The wrapper takes the real function's parameters and forwards them all.jest.mock('@/orders/saveOrder', () => ({saveOrder: (params: { id: string }) => mockSaveOrder(params),}));const mockQueryResult = {data: 'p.png',isLoading: false,refetch: jest.fn(),} as unknown as Record<string, unknown>;// A test whose subject reads code as text passes that code in as data. This is// a mention of an untyped spy, not one — the rule reads what the file does, not// what it quotes.const sampleLine = 'const getAvatar = jest.fn();';describe('subject', () => {test('reads the locale and the stubbed field', () => {mockGetLocale.mockReturnValue('en-GB');expect(mockGetLocale() + mockQueryResult.data).toBe('en-GBp.png');});test('carries its sample line untouched', () => {expect(sampleLine).toContain('getAvatar');});});
Configure
- BlockDefault
"blocking"Stops a run when a file the run changed breaks the rule. - Advise
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"test-mock-untyped": "blocking"}}