API Route Testing
This page documents how the dapp’s Next.js API routes are tested using Vitest. It focuses on:
- How route tests are structured.
- What is mocked vs. what is exercised for real.
- How error, auth, and rate-limit paths are covered.
It uses two core endpoints as running examples:
/api/gate-access– token-gated access and SIWE/JWT flows./api/token-status/[tokenId]– token status and usage checks.
💡
This page assumes familiarity with Next.js route handlers and Vitest basics. It focuses on project-specific design: how the routes are tested in this codebase.
Overview
Overview
API route tests live alongside the route handlers under app/api:
At this layer, tests:
- Import route handlers (
GET,POST, etc.) directly from../route. - Construct
NextRequestinstances with realistic URLs, methods, headers, and JSON bodies. - Mock:
- viem clients and chain definitions.
- Prisma access (
getTokenModel,getChainConfig, etc.). - Rate limiting helpers.
- JWT helpers and SIWE helpers.
- HTTP utilities for CORS, problem responses, and validation.
- Assert on:
- HTTP status codes.
- Response JSON (problem+json shape, domain-specific fields).
- Important headers (rate limit metadata,
Retry-After).
The priority is to cover behavioural branches: validation, authentication paths, on-chain vs DB decisions, and error handling.
Summary
- API route tests import route handlers directly and exercise them via
NextRequestinstances. - External dependencies (viem, Prisma, rate limiting, SIWE, JWT, content generation) are fully mocked to keep tests deterministic.
- Suites are structured by behavioural domains: validation, auth, rate limits, DB/chain decisions, and error handling.
- The
gate-accessandtoken-statussuites are reference implementations for testing new routes in this repo.
For OpenAPI contract-level guarantees, see the API Contract & OpenAPI Testing page.