API Integration Testing
Build robust test suites for your APIs with integration, contract, and E2E tests.
Testing Pyramid for APIs
| Level | What It Tests | Speed | Isolation |
|---|
| Unit | Handlers, validators, utils | Fast | High |
| Integration | API + database, services | Medium | Medium |
| Contract | API shape vs OpenAPI spec | Fast | High |
| E2E | Full stack, real DB | Slow | Low |
Key Principles
- Test API behavior through HTTP requests, not internal function calls
- Use real database for integration tests (SQLite or test containers)
- Validate response shapes against OpenAPI spec with contract tests
- Isolate external services with MSW or similar mock servers
Quick Start Checklist
- Set up test framework (Vitest + Supertest recommended)
- Configure test database (separate from dev)
- Create test factories for data setup
- Write integration tests for each endpoint (happy + error paths)
- Add contract tests against OpenAPI spec
- Configure CI pipeline for automated test runs
References