Performance: Integration testing patterns
MEDIUM IMPACT
Integration testing patterns affect the speed of test execution and resource usage during development, impacting developer feedback loops and CI pipeline efficiency.
import { setupTestDB, teardownTestDB } from './testUtils'; import nock from 'nock'; describe('User API', () => { beforeAll(async () => await setupTestDB()); afterAll(async () => await teardownTestDB()); beforeEach(() => { nock('http://real-api.com') .post('/users') .reply(201, { id: 1, name: 'Alice' }); }); it('creates a user', async () => { const response = await fetch('http://real-api.com/users', { method: 'POST', body: JSON.stringify({name: 'Alice'}) }); expect(response.status).toBe(201); }); });
describe('User API', () => { it('creates a user', async () => { await db.connect(); await db.clear(); const response = await fetch('http://real-api.com/users', { method: 'POST', body: JSON.stringify({name: 'Alice'}) }); expect(response.status).toBe(201); await db.disconnect(); }); });
| Pattern | Network Calls | Resource Usage | Test Runtime | Verdict |
|---|---|---|---|---|
| Real external calls per test | Multiple real HTTP requests | High (DB connect/disconnect each test) | Slow (seconds per test) | [X] Bad |
| Mocked external calls with shared DB setup | Mocked HTTP requests | Low (shared DB connection) | Fast (milliseconds per test) | [OK] Good |