Introduction: Why You Need TypeScript Integration Testing with Docker
Integration testing is a key development stage that checks the interaction between different modules of your application. Unlike unit tests, which isolate a single component, integration tests verify how the database, API, external services, and business logic work together.
Using Docker in TypeScript integration testing solves several problems:
- Environment isolation — tests run in identical conditions on all machines
- Dependency management — databases, queues, and caches are started automatically
- Reproducibility — tests produce the same results locally and in CI/CD
- Fast recovery — containers are recreated in seconds
In this article, we will walk through a practical example of setting up integration testing for a TypeScript application using Docker, Jest, and Supertest.
1. Project Setup and Dependency Installation
Let's start by creating a basic TypeScript project. Make sure you have Node.js (v18+) and Docker installed.
mkdir typescript-docker-testingcd typescript-docker-testingnpm init -ynpm install express typescript ts-node @types/node @types/expressnpm install --save-dev jest ts-jest @types/jest supertest @types/supertestCreate tsconfig.json:
{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true, "skipLibCheck": true }, "include": ["src/**/*"], "exclude": ["node_modules", "**/*.test.ts"]}Configure Jest in package.json:
"jest": { "preset": "ts-jest", "testEnvironment": "node", "testMatch": ["**/*.test.ts"]}2. Creating a Simple Express Application
Let's create the folder structure and a simple application that will interact with a PostgreSQL database.
mkdir srcmkdir src/configmkdir src/modelsmkdir src/routesmkdir src/__tests__src/config/database.ts — database connection configuration:
import { Pool } from 'pg';
const pool = new Pool({ host: process.env.DB_HOST || 'localhost', port: Number(process.env.DB_PORT) || 5432, database: process.env.DB_NAME || 'testdb', user: process.env.DB_USER || 'testuser', password: process.env.DB_PASSWORD || 'testpass'});
export default pool;src/app.ts — main application:
import express from 'express';import userRoutes from './routes/users';
const app = express();app.use(express.json());app.use('/api/users', userRoutes);
export default app;src/index.ts — entry point:
import app from './app';
const PORT = process.env.PORT || 3000;app.listen(PORT, () => { console.log(`Server running on port ${PORT}`);});src/routes/users.ts — simple user route:
import { Router, Request, Response } from 'express';import pool from '../config/database';
const router = Router();
router.get('/', async (req: Request, res: Response) => { try { const result = await pool.query('SELECT * FROM users'); res.json(result.rows); } catch (error) { res.status(500).json({ error: 'Database error' }); }});
router.post('/', async (req: Request, res: Response) => { const { name, email } = req.body; try { const result = await pool.query( 'INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *', [name, email] ); res.status(201).json(result.r
```