0Pricing
TypeScript Academy · Lesson

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

  1. Vitest/Jest setup for TS
  2. Typing tests & mocks; expectTypeOf
  3. E2E typing glimpses (Playwright/Cypress)
← Back to TypeScript Academy