Skip to main content

Testing Guide

HostMetrics uses Vitest for unit and integration tests and Playwright for end-to-end browser tests.

Test Stack

ToolPurposeConfig File
VitestUnit and integration testsvitest.config.ts
PlaywrightEnd-to-end browser testsplaywright.config.ts
jsdomDOM environment for VitestSet in Vitest config

Directory Structure

Running Tests

Unit Tests

End-to-End Tests

Vitest Configuration

Key settings from vitest.config.ts:
  • Environment: jsdom (simulates browser DOM)
  • Coverage thresholds: 80% for branches, functions, lines, and statements
  • Path aliases: @/ maps to src/ (mirrors the app’s tsconfig)
  • Setup files: Global test setup for mocks and environment

Test File Naming

What you’re testingFile name patternLocation
ComponentComponentName.test.tsxsrc/__tests__/unit/
HookhookName.test.tssrc/__tests__/unit/
Utility functionutilName.test.tssrc/__tests__/unit/
E2E user flowfeature.spec.tse2e/specs/

Mock Patterns

Mocking the Supabase Client

The shared Supabase mock lives at src/__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:
ProjectBrowserViewport
chromiumChromiumDesktop
firefoxFirefoxDesktop
webkitWebKit/SafariDesktop
mobile-chromeMobile Chrome390x844
mobile-safariMobile Safari390x844

Auth Setup

E2E tests authenticate before running specs using auth.setup.ts:

Writing E2E Specs

Pre-Commit Test Commands

Before creating a PR, always run:
See the PR Checklist for the full verification workflow.