Mock Typing
Mock typing is giving a fake the same type as the real collaborator so the test cannot call methods that do not exist or return the wrong shape. Untyped mocks are any.
Detailed Explanation
A mock is a value you pass in place of a dependency. If that value is any, the test compiles no matter what you pass, and the system under test loses checking at the boundary. The fix is to start from the real type.
Prefer an interface on the collaborator (UserRepo). A fake is then an object literal that implements the interface, or a satisfies UserRepo. Missing methods fail in the test file. Wrong return types fail. You do not need a mocking library for that.
When you do need a library (jest.fn, vi.fn) — because you want toHaveBeenCalledWith — type the mock with the method's type:
const find: UserRepo["find"] = vi.fn();
Or vi.fn<UserRepo["find"]>(). mockResolvedValue then accepts User | undefined, not any. toHaveBeenCalledWith checks argument types in recent Jest/Vitest typings.
Partial<UserRepo> is a common shortcut and a hole: you can omit save and then the SUT calls save at runtime. Use Partial only if the SUT cannot reach the omitted methods in that test, and you document that. A better helper is function fakeRepo(partial: Partial<UserRepo>): UserRepo that fills defaults.
Module mocks (jest.mock("./repo")) replace the module's exports. Types still come from the original module unless you write a manual mock in __mocks__ that exports the same shapes. jest.mocked(repo) / vi.mocked(repo) wraps the imported value in a type that has mock methods. Without that wrapper, TypeScript thinks you have the real UserRepo and mockResolvedValue does not exist on the type.
Deep mocks (mockDeep<PrismaClient>()) generate a recursive mock. They compile. They also accept almost anything at the leaves if the generator uses any. Use them for generated clients you cannot construct, and still type the return of the one method you care about.
Do not as unknown as UserRepo a {}. That is the assertion you are trying to get rid of.
Code Examples
1. A fake that satisfies the interface — no library.
// 1. A fake that satisfies the interface — no library.
interface UserRepo {
find(id: string): Promise<{ id: string } | undefined>;
}
const fake: UserRepo = {
async find(id) {
return id === "u1" ? { id } : undefined;
},
};2. A typed fn mock.
// 2. A typed fn mock.
const find = vi.fn<UserRepo["find"]>();
find.mockResolvedValue({ id: "u1" });
// find.mockResolvedValue(1);
// Error: number is not assignable to { id: string } | undefined.3. Defaulting helper so Partial cannot ship a hole.
// 3. Defaulting helper so Partial cannot ship a hole.
function fakeRepo(partial: Partial<UserRepo> = {}): UserRepo {
return {
async find() {
return undefined;
},
...partial,
};
}Common Gotchas
Untyped `jest.fn()` / `vi.fn()` returns `any`
the SUT's parameter type will not save you if you pass the mock in a hole (`deps as any`).
`Partial<Repo>` lets you omit methods the SUT still calls
fill defaults in a helper.
`vi.mocked` / `jest.mocked` is what adds mock methods to the type
without it, `mockResolvedValue` is a type error on the real function type.
`as UserRepo` on an empty object compiles and then throws
use `satisfies` or a helper.