0Pricing
TypeScript Academy · Lesson

Typing tests & mocks; expectTypeOf

Use expectTypeOf for static checks, and type-safe mocks in Vitest/Jest to prevent brittle tests.

Typing tests & mocks; expectTypeOf is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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()

Advanced assertions

Useful patterns: resolving return types with Awaited<ReturnType<...>>, literal and union comparisons.

import { expectTypeOf } from "vitest";

interface User { id: number; name: string }
function getUser(): Promise<User> { return Promise.resolve({ id:1, name:"A" }) }

// unwrap Promise
type Resolved = Awaited<ReturnType<typeof getUser>>
expectTypeOf<Resolved>().toMatchTypeOf<User>()

// literal + union
const status = "ok" as const
expectTypeOf(status).toEqualTypeOf<"ok">()

Vitest mocks

Type the mock correctly by providing a signature for vi.fn; incorrect arguments will be caught in compilation.

import { vi, describe, it, expect } from "vitest";

function fetchUser(id: number): Promise<string> {
  return Promise.resolve(`user-${id}`)
}

// typed mock
const mockFetch: ReturnType<typeof vi.fn<[(id: number), Promise<string>]>> = vi.fn(async (id: number) => `user-${id}`)

it("uses typed mock", async () => {
  const name = await mockFetch(5)
  expect(name).toBe("user-5")
  // mockFetch("x") // TS error: id must be number
})

Jest mocks

In Jest, enforce the call signature by writing jest.fn<Signature>(); disambiguate the mock type with MockedFunction.

import { jest } from "@jest/globals";

function times(a: number, b: number) { return a * b }

const mul = jest.fn<(a:number,b:number)=>number>((a,b)=>a*b)

mul(2,3) // ok
// mul("2",3) // TS error

// MockedFunction utility
type MulMock = jest.MockedFunction<typeof mul>
const _check: MulMock = mul

Tips

Separate value validation (runtime) from type validation (compile-time); they are different layers.

// value assertions
import { expect } from "vitest";
expect(2 + 3).toBe(5)

// type assertions (compile-time)
import { expectTypeOf } from "vitest";
expectTypeOf(2 + 3).toBeNumber()

// don't confuse with runtime validators like zod

Question

Quick check: What does expectTypeOf check during a test run?

Recap

Recap: expectTypeOf — compile-time type guarantee; vi.fn/jest.fn — type-safe mocks with signature.

Frequently asked questions

Is the “Typing tests & mocks; expectTypeOf” lesson free?

Yes — the full text of “Typing tests & mocks; expectTypeOf” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Typing tests & mocks; expectTypeOf”?

Use expectTypeOf for static checks, and type-safe mocks in Vitest/Jest to prevent brittle tests. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Typing tests & mocks; expectTypeOf” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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