Skip to content

no-unsafe-mock-casting

Disallow casting to mock types like jest.Mock or vi.Mock. Use vi.mocked() or jest.mocked() instead.

Why

A type assertion can claim an unmocked value is a mock and bypass checking between the original callable and the mock API.

Fix

Create the mock or spy first, then use the framework's mocked helper to preserve the original value's type. The helper does not create or verify a runtime mock.

Examples

Before — flagged Do not assert that a value is a mock
src/client.test.ts
import type * as vi from "vitest";
const m = myFn as vi.Mock;
After — preferred Use the framework helper
src/client.test.ts
import { vi } from "vitest";
const client = { read: () => "value" };
vi.spyOn(client, "read");
const m = vi.mocked(client.read);

References