
Summary
Key points of this document
- Where are the design challenges?
- How to define boundaries
- Details that matter in implementation
Overview
API testing needs to check not only if internal functions are correct, but also if the contract with users has changed. With Vitest, you can test the boundaries of input, output, and errors. API contract tests are a mechanism to quickly find discrepancies between frontend and backend expectations, not just the correctness of the implementation.
Where are the design challenges?
Even if types pass in TypeScript, the contract as an HTTP API can change. If status codes, error bodies, date formats, or null handling change, the frontend or external integrations will break.
Writing heavy integration tests for all APIs increases execution time. On the other hand, for critical update-related or authentication-involved APIs, unit tests alone are insufficient. Test granularity appropriate for the risk is necessary.
Even if you write frontend and API in TypeScript, the runtime response is not always type-safe. Changes in API field names or error formats can break the UI even if compilation passes.
How to define boundaries
With Vitest, we invoke the Hono app in memory and validate it as an HTTP response. We prepare cases for success, invalid input, insufficient permissions, non-existent targets, and state conflicts.
In design, we fix API contracts not only with type definitions but also with runtime schemas and tests. We use Vitest to call Hono handlers and API clients, checking if responses meet the schema and if status codes and error codes are as expected.
Details that matter in implementation
Test data is created with factories, and if a DB is used, it's isolated per test. Instead of unit testing Zod schemas, we send requests to actual routes and verify the response format. Error codes are treated as string constants.
Place Zod schemas at the API response boundary, and in tests, parse both success and failure responses. Replace DBs and external APIs with test adapters to focus on contract verification.
- Schema-ize not only responses that the UI depends on, but also the shape of error bodies.
- Distinguish between adding optional fields and changing required fields, and find breaking changes during review.
- Include representative permission states in contract tests.
Observe how things break
In CI, run API contract tests quickly and separate them from heavy E2E tests. Full response snapshots are fragile to changes, so explicitly validating important fields is easier to review.
In verification, we check if API implementation tests, API client tests, and UI-side mock data refer to the same schema. When a contract test fails, we clarify in the PR whether it's an implementation bug or a contract change.
Discarded options and tradeoffs
Increasing contract tests requires test modifications when changes occur. However, this is also an opportunity to explicitly indicate that the API contract has changed. Prioritizing externally visible behavior over internal implementation tests is effective.
Writing detailed contract tests increases modifications during changes, but it prevents unintended breaking changes. Instead of fixing all fields, it's realistic to prioritize parts that the UI or external integrations depend on.
Decision criteria for the team
Vitest can be used not only for function testing but also for regression detection of API contracts. By verifying inputs and outputs at HTTP boundaries, it becomes robust against changes by AI agents or multiple developers.
Vitest's contract tests are not backend unit tests. By verifying the agreement with the API consumer at runtime, discrepancies that types alone cannot catch can be reduced.


