Skip to main content
Unit tests use Vitest with a jsdom environment. They test pure TypeScript and client-side DOM code in isolation — no browser launch, no Sanity API calls, no Astro build required.

Running unit tests

Coverage reports output to test-results/unit-coverage/ (HTML + lcov) and JUnit XML to test-results/unit-results.xml.

What’s tested

Coverage is collected from src/lib/**/*.ts and src/scripts/**/*.ts only. Test files, type declarations, and mock data directories are excluded.

File locations

Vitest also picks up integration tests from tests/integration/**/*.test.ts via the include glob in vitest.config.ts.

vitest.config.ts

Key patterns

sanity:client mock

Vitest aliases the sanity:client virtual module (an Astro/Sanity internal) to a hand-written mock at src/lib/__tests__/__mocks__/sanity-client.ts. This allows GROQ query tests to run without a live Sanity project or network access.

jsdom environment

DOM-dependent tests (client-side scripts, middleware cookie parsing) annotate with the jsdom environment pragma:

Path aliases

@/ resolves to astro-app/src/ in test files, matching the same alias used in Astro’s tsconfig.json:

Example: utility test

Example: middleware test

The middleware test file (src/__tests__/middleware.test.ts) covers the unified auth routing layer — public route pass-through, portal/student session validation, KV cache hits/misses, Durable Object rate limiting, and dev mode bypass:
Run npm run test:unit:watch during development. Vitest re-runs only the affected test files on save, giving sub-second feedback.

Dependencies