Integration Testing TypeScript with Docker: Advanced Level

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Integration Testing TypeScript with Docker: Advanced Level

\n\n

Integration testing is a key stage for verifying the interaction of system components. For TypeScript applications, especially those with a microservice architecture, Docker becomes an indispensable tool. In this article, we will explore advanced techniques for setting up and running integration tests using Docker, which will help you improve reliability and development speed.

\n\n

1. Why Docker for TypeScript Integration Testing?

\n

Docker allows you to isolate test environments, guaranteeing their reproducibility. Instead of manually configuring databases, message queues, and other dependencies, you run them in containers. This is especially important for TypeScript projects, where typing can hide runtime errors that surface precisely during integration.

\n
    \n
  • Isolation: each test runs in a clean environment
  • \n
  • Speed: containers start in seconds
  • \n
  • Parallelism: you can test different dependency versions simultaneously
  • \n
\n\n

2. Setting up Docker Compose for the Test Environment

\n

Let's create a docker-compose.test.yml that spins up PostgreSQL, Redis, and the application itself for testing:

\n
version: '3.8'\nservices:\n  db-test:\n    image: postgres:15-alpine\n    environment:\n      POSTGRES_DB: testdb\n      POSTGRES_USER: test\n      POSTGRES_PASSWORD: testpass\n    ports:\n      - "5433:5432"\n    tmpfs: /var/lib/postgresql/data\n\n  redis-test:\n    image: redis:7-alpine\n    ports:\n      - "6380:6379"\n\n  app-test:\n    build:\n      context: .\n      dockerfile: Dockerfile.test\n    environment:\n      DATABASE_URL: postgres://test:testpass@db-test:5432/testdb\n      REDIS_URL: redis://redis-test:6379\n    depends_on:\n      - db-test\n      - redis-test\n    command: ["npm", "run", "test:integration"]
\n

Here we use tmpfs for the database so that data is not persisted between runs, which speeds up tests.

\n\n

3. Advanced TypeScript Test Setup with TestContainers

\n

The testcontainers library allows you to manage containers directly from TypeScript code. This provides flexibility: you can spin up containers only for specific test scenarios.

\n

Installation:

\n
npm install --save-dev testcontainers
\n

Example of an integration test with a dynamic container:

\n
import { GenericContainer, StartedTestContainer } from 'testcontainers';\nimport { Client } from 'pg';\nimport { UserService } from './user-service';\n\nlet container: StartedTestContainer;\nlet client: Client;\n\nbeforeAll(async () => {\n  // Start a PostgreSQL container\n  container = await new GenericContainer('postgres:15-alpine')\n    .withEnvironment({\n      POSTGRES_DB: 'test',\n      POSTGRES_USER: 'test',\n      POSTGRES_PASSWORD: 'test'\n    })\n    .withExposedPorts(5432)\n    .start();\n\n  // Connect to the test database\n  const host = container.getHost();\n  const port = container.getMappedPort(5432);\n  client = new Client({\n    host,\n    port,\n    database: 'test',\n    user: 'test',\n    password: 'test'\n  });\n  await client.connect();\n\n  // Run migrations\n  await client.query(`\n    CREATE TABLE users (\n      id SERIAL PRIMARY KEY,\n      email VARCHAR(255) UNIQUE NOT NULL,\n      name VARCHAR(255)\n    )\n  `);\n});\n\nafterAll(async () => {\n  await client.end();\n  await container.stop();\n});\n\ntest('UserService.createUser creates a user in the database', async () => {\n  const userService = new UserService(client);\n  const user = await userService.createUser({\n    email: 'test@example.com',\n    name: 'Test User'\n  });\n\n  const result = await client.query('SELECT * FROM users WHERE email = $1', ['test@example.com']);\n  expect(result.rows[0]).toMatchObject({\n    email: 'test@example.com',\n                

Blogs

Book Recommendations