Building REST APIs with Express.jsLesson 3.5
Testing REST APIs: Unit Tests and Integration Tests
API testing, Jest, Supertest, unit testing controllers, integration testing endpoints, test database, test coverage, mocking, TDD
Testing REST APIs: Unit Tests and Integration Tests
Untested REST APIs are technical debt waiting to explode. Every endpoint change risks breaking existing clients. A comprehensive test suite catches regressions before deployment, documents expected behavior, and gives you confidence to refactor.
Setting Up Jest and Supertest
npm install --save-dev jest supertest
Add to package.json:
"scripts": {
"test": "jest --coverage",
"test:watch": "jest --watchAll"
},
"jest": {
"testEnvironment": "node",
"coverageThreshold": {
"global": { "lines": 80 }
}
}Integration Testing with Supertest
const request = require('supertest');
const app = require('../src/app');
describe('GET /api/users', () => {
it('should return 200 and a list of users', async () => {
const res = await request(app)
.get('/api/users')
.set('Authorization', `Bearer ${testToken}`);
expect(res.statusCode).toBe(200);
expect(res.body.success).toBe(true);
expect(Array.isArray(res.body.data)).toBe(true);
});
it('should return 401 without authentication', async () => {
const res = await request(app).get('/api/users');
expect(res.statusCode).toBe(401);
});
});What to Test
Test the happy path (successful operations), all error conditions (400, 401, 403, 404, 409, 422), edge cases (empty arrays, null values, boundary values), and authorization (that users cannot access resources they should not). Every endpoint should have at least three tests: success, not-found, and unauthorized.
