Skip to content

no-unsafe-test-double-cast

Disallow mock-backed test doubles that bypass collaborator contracts through double assertions.

Why

Casting a partial object through unknown hides missing or stale collaborator members, so production interface changes can compile while the test double silently drifts.

Fix

Define the smallest injected port and implement a typed recording fake, or make the fixture satisfy the real contract without passing through unknown.

Examples

Before — flagged Do not hide a partial mock behind unknown
service.test.ts
import { vi } from "vitest";
const client = { read: vi.fn() } as unknown as Client;
After — preferred Use a typed recording fake
service.test.ts
const client = { read: async () => ({ id: "1" }) } satisfies Pick<Client, "read">;