Integration Testing
What Integration Tests Validate
Integration tests confirm that boundaries between modules are correct: SQL queries work against a real schema, JSON serialization matches clients, HTTP handlers produce the right status codes, and error handling behaves predictably.
When Integration Tests Are Worth It
- Database queries with joins, transactions, and constraints
- Authentication flows (token parsing, cookie behavior)
- Request validation + error formatting
- Timeout and retry behavior around dependencies
Scope Control: Keep Them Focused
Integration tests should be small and targeted. If each test requires a full environment, they become slow and fragile. Prefer running dependencies in containers and resetting state per test or per suite.
Database Integration Pattern
A common approach is using a dedicated test database and wrapping each test in a transaction that is rolled back, or truncating tables between tests. The goal is deterministic state.
Example: Transaction Rollback Concept
beforeEach(async () => {
await db.query('BEGIN');
});
afterEach(async () => {
await db.query('ROLLBACK');
});
Production Guidance
Focus integration tests on the critical paths that break in real life: schema drift, migrations, connection pooling misconfigurations, and unexpected serialization issues. These are the failures that cause incidents.