Skip to content

Testing Guide

Framework

Vitest across all packages — TypeScript-native, ESM-first, fast.

PackageEnvironmentKey Libraries
workers/apinodeVitest, Hono app.request()
apps/webjsdomVitest, React Testing Library, @testing-library/jest-dom
packages/sharednodeVitest

Directory Structure

workers/api/src/__tests__/
__mocks__/env.ts # KV, R2, Env factories
__fixtures__/html-samples.ts
middleware/auth.test.ts
routes/auth.test.ts
routes/files.test.ts
routes/convert.test.ts
routes/export.test.ts
routes/benchmark.test.ts
services/wcag-validator.test.ts
services/html-analyzer.test.ts
services/ux-optimizer.test.ts
services/math-detector.test.ts
services/claude-converter.test.ts
services/marker-converter.test.ts
services/unpdf-claude-converter.test.ts
services/mathpix-pdf.test.ts
services/image-enhancer.test.ts
services/benchmark-orchestrator.test.ts
services/axe-validator.test.ts
services/pdf-generator.test.ts
utils/crypto.test.ts
utils/response.test.ts
index.test.ts
apps/web/src/__tests__/
setup.ts # RTL + jsdom + matchMedia mock
lib/utils.test.ts
lib/mathpix-storage.test.ts
lib/api.test.ts
lib/auth-context.test.tsx
components/upload/DropZone.test.tsx
packages/shared/src/__tests__/
constants.test.ts
benchmark-types.test.ts

Running Tests

Terminal window
# All packages (via Turborepo)
npm run test:run
# With coverage
npm run test:coverage
# Watch mode
npm test
# Single package
cd workers/api && npx vitest run
cd apps/web && npx vitest run
cd packages/shared && npx vitest run

Mock Strategy

DependencyApproach
Cloudflare KVIn-memory Map-backed mock (createMockKV())
Cloudflare R2In-memory Map-backed mock (createMockR2())
Cloudflare Puppeteervi.mock('@cloudflare/puppeteer') with mock page/browser
Anthropic SDKvi.mock('@anthropic-ai/sdk') with mock messages.create
unpdfvi.mock('unpdf')
External fetch (Mathpix, Marker)vi.stubGlobal('fetch', mockFetch)
Supabase clientvi.mock('@/lib/supabase')
localStoragejsdom’s built-in window.localStorage
Auth (JWTs)Real HMAC-SHA256 signed JWTs via createTestJwt() helper

Adding New Tests

  1. Create a .test.ts (or .test.tsx) file in the appropriate __tests__/ directory
  2. Import from vitest: { describe, it, expect, vi, beforeEach }
  3. Use existing mock factories from __mocks__/env.ts for API tests
  4. For route tests, use the createTestJwt pattern from existing route tests
  5. Run npx vitest run in the package to verify

Coverage Targets

AreaTarget
workers/api/src/services/80%
workers/api/src/routes/70%
workers/api/src/middleware/85%
workers/api/src/utils/90%
packages/shared/95%
apps/web/src/lib/70%
apps/web/src/components/60%
Overall75%