Typing tests & mocks; expectTypeOf
Use expectTypeOf for static checks, and type-safe mocks in Vitest/Jest to prevent brittle tests.
Intro
Goal: Assert types with expectTypeOf and write type-safe mocks so refactors don't silently break tests.
expectTypeOf basics
expectTypeOf asserts types at compile-time in Vitest; no runtime cost, but catches mismatches early.
// Vitest provides expectTypeOf
import { expectTypeOf } from "vitest";
function fullName(first: string, last: string) {
return `${first} ${last}`
}
// static checks (compile-time)
expectTypeOf(fullName).toBeFunction()
expectTypeOf(fullName("Ada","Lovelace")).toBeString()All lessons in this course
- Vitest/Jest setup for TS
- Typing tests & mocks; expectTypeOf
- E2E typing glimpses (Playwright/Cypress)