Testing & Deployment
Test Node services with Jest or Vitest, containerize with Docker, and deploy to cloud platforms reliably.
Unit and Integration Testing
Jest and Vitest run unit tests with mocking built in. Test pure functions without I/O and mock database modules at repository boundaries.
Supertest hits Express apps in-memory without binding ports. Spin up Testcontainers for integration tests against real PostgreSQL or Redis.
Separate fast unit suites from slower integration jobs in CI for quick feedback.
- Reset database state between integration tests
- Use NODE_ENV=test with separate config
- Measure coverage but prioritize critical paths over 100%
import request from 'supertest';
import { app } from '../app.js';
test('GET /health', async () => {
const res = await request(app).get('/health');
expect(res.status).toBe(200);
});Docker and CI
Multi-stage Dockerfiles install dependencies, build TypeScript, and copy minimal runtime artifacts into slim production images. Run as non-root users.
CI pipelines lint, test, build images, and deploy on green main. Cache npm dependencies for speed.
Health checks and graceful shutdown hooks integrate with orchestrators like Kubernetes.
- Use .dockerignore to exclude node_modules and tests
- Pass secrets via orchestrator secrets, not ENV in Dockerfile
- Tag images with git SHA for traceability
FROM node:20-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:20-alpine WORKDIR /app COPY --from=build /app/dist ./dist CMD ["node", "dist/index.js"]
Production Deployment
Deploy to AWS ECS, Fly.io, Railway, Render, or traditional VMs with systemd. Use reverse proxies (nginx, Caddy) for TLS termination when not handled by platform.
Configure NODE_ENV=production, logging aggregation, and alerts on error rates and latency SLOs.
Run database migrations as separate release steps before switching traffic to new app versions.
- Implement /health and /ready endpoints for load balancers
- Use structured JSON logging for searchability
- Practice rollback procedures before launch day