Performance: Testing API routes
MEDIUM IMPACT
Testing API routes affects development speed and reliability but can indirectly impact user experience by preventing slow or broken API responses.
import request from 'supertest'; import app from '../../pages/api/hello'; describe('API route /api/hello', () => { it('responds with 200 and JSON', async () => { const response = await request(app).get('/api/hello'); expect(response.status).toBe(200); expect(response.body).toHaveProperty('message'); }); });
import handler from '../../pages/api/hello'; test('API returns 200', async () => { const req = {}; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() }; await handler(req, res); expect(res.status).toHaveBeenCalledWith(200); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual mocks for API testing | 0 (no DOM) | 0 | 0 | [X] Bad |
| HTTP simulation with supertest | 0 (no DOM) | 0 | 0 | [OK] Good |