Lab 11: Testing Architecture

Time: 60 minutes | Level: Architect | Docker: node:20-alpine

Overview

TypeScript testing architecture: Vitest with full type inference, type-level testing with expectTypeOf, MSW typed request handlers, typed test fixtures with factory pattern, and property-based testing with fast-check.


Step 1: Vitest Setup

// vitest.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,              // No import needed for describe/test/expect
    environment: 'node',        // or 'jsdom' for browser-like
    typecheck: {
      enabled: true,            // Run type tests with tsc
      tsconfig: './tsconfig.test.json',
    },
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: { lines: 80, functions: 80, branches: 70 },
    },
    setupFiles: ['./src/test/setup.ts'],
  },
});

Step 2: Type-Level Tests with expectTypeOf


Step 3: MSW — Typed Request Handlers


Step 4: Typed Test Fixtures — Factory Pattern


Step 5: Property-Based Testing with fast-check


Step 6: Integration Test with Dependency Injection


Step 7: Snapshot Testing with Type Checks


Step 8: Capstone — Property-Based Tests

📸 Verified Output:


Summary

Tool
Purpose
TypeScript Benefit

Vitest

Unit/integration tests

Full TS inference

expectTypeOf

Type-level tests

Catch type regressions

MSW

API mocking

Typed request/response

Factory pattern

Test data

Type-safe overrides

fast-check

Property-based

Generate edge cases

vi.fn()

Mocking

Type-preserving mocks

toMatchInlineSnapshot

Regression

Visual type output

Last updated