Testing APIs
Tests are what let you change an API with confidence. Three layers, each catching a different class of bug, give good coverage without redundant effort.
The layers
Section titled “The layers”flowchart TD U[Unit: handlers, validation, pure logic - many, fast] --> I[Integration: real routes end-to-end - fewer] I --> C[Contract: responses match the OpenAPI spec - focused]
- Unit tests — exercise pure pieces: a validator, a pagination helper, an error mapper. Fast and numerous.
- Integration tests — send real requests through the actual router and assert status, headers, and body. These catch wiring bugs unit tests miss.
- Contract tests — assert that responses conform to the OpenAPI schema, so the implementation and the published contract never drift.
An integration-style assertion
Section titled “An integration-style assertion”Frameworks like Hono expose app.request(...), so you can call routes in-process without a network:
What to assert
Section titled “What to assert”For each endpoint, test the happy path and the failure paths: the right status code, the response shape, the error body for invalid input (does it match your problem+json?), auth required where expected, and pagination metadata on collections.