Testing Guide
HostMetrics uses Vitest for unit and integration tests and Playwright for end-to-end browser tests.Test Stack
| Tool | Purpose | Config File |
|---|---|---|
| Vitest | Unit and integration tests | vitest.config.ts |
| Playwright | End-to-end browser tests | playwright.config.ts |
| jsdom | DOM environment for Vitest | Set in Vitest config |
Directory Structure
Running Tests
Unit Tests
End-to-End Tests
Vitest Configuration
Key settings fromvitest.config.ts:
- Environment:
jsdom(simulates browser DOM) - Coverage thresholds: 80% for branches, functions, lines, and statements
- Path aliases:
@/maps tosrc/(mirrors the app’s tsconfig) - Setup files: Global test setup for mocks and environment
Test File Naming
| What you’re testing | File name pattern | Location |
|---|---|---|
| Component | ComponentName.test.tsx | src/__tests__/unit/ |
| Hook | hookName.test.ts | src/__tests__/unit/ |
| Utility function | utilName.test.ts | src/__tests__/unit/ |
| E2E user flow | feature.spec.ts | e2e/specs/ |
Mock Patterns
Mocking the Supabase Client
The shared Supabase mock lives atsrc/__tests__/mocks/supabase.ts. Use it in unit tests:
Mocking Hooks in Component Tests
When testing components, mock the hooks they depend on:Mocking Next.js Router
Writing Unit Tests
Follow this structure for hook tests:Playwright E2E Tests
Browser Projects
Playwright is configured with 5 browser projects:| Project | Browser | Viewport |
|---|---|---|
chromium | Chromium | Desktop |
firefox | Firefox | Desktop |
webkit | WebKit/Safari | Desktop |
mobile-chrome | Mobile Chrome | 390x844 |
mobile-safari | Mobile Safari | 390x844 |
Auth Setup
E2E tests authenticate before running specs usingauth.setup.ts: