DEV Community

Roberto Luna
Roberto Luna

Posted on

Fixing Docker‑Compose Test Environment: Reading `DB_HOST` from `process.env` in Jest Setup

Fixing Docker‑Compose Test Environment: Reading DB_HOST from process.env in Jest Setup

TL;DR: I updated the Jest test‑setup file to pull the database host from process.env.DB_HOST instead of a hard‑coded value. This stopped the API container from crashing during CI runs and aligned the local Docker‑Compose stack with the test suite.


The Problem

Our monorepo (content-automation) contains a full‑stack API (apps/api) that is exercised by integration tests in apps/api/src/__tests__/setup.ts. When I spun up a fresh Docker‑Compose stack for the VS project, the test runner threw the following error:

Error: connect ECONNREFUSED 127.0.0.1:5432
    at Connection._handleConnectError (node_modules/pg/lib/client.js:207:12)
    at Timeout._onTimeout (node_modules/pg/lib/client.js:215:7)
Enter fullscreen mode Exit fullscreen mode

The stack was configured to expose Postgres on port 5433 (see docker-compose.yml), but the test setup was still trying to connect to the default localhost:5432. The mismatch caused the API container to fail during the npm test phase, breaking the CI pipeline and halting the weekly content‑automation run.

What I Tried First

My first instinct was to patch the Docker‑Compose file, forcing Postgres back to 5432 just for the test container. I added an override service:

services:
  api-test:
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
Enter fullscreen mode Exit fullscreen mode

That approach worked locally, but it introduced a configuration drift: production and staging environments still run on 5433. Maintaining two different ports in the repo was a recipe for future bugs, and the CI environment (which builds the exact same docker-compose.yml as production) still failed because the override wasn't applied there.

The Implementation

The clean fix was to make the test setup environment‑aware. The change lives in apps/api/src/__tests__/setup.ts. Below is the diff from commit d9fdc189:

--- a/apps/api/src/__tests__/setup.ts
+++ b/apps/api/src/__tests__/setup.ts
@@ -1,12 +1,14 @@
-import { createConnection } from 'typeorm';
-// Hard‑coded DB host (old)
-const DB_HOST = 'localhost';
+import { createConnection } from 'typeorm';
+// New: read DB host from environment, fallback to localhost for backward compatibility
+const DB_HOST = process.env.DB_HOST ?? 'localhost';

 export const testConnection = async () => {
   await createConnection({
     type: 'postgres',
-    host: DB_HOST,
+    host: DB_HOST, // now respects the Docker‑Compose DB_HOST variable
     port: Number(process.env.DB_PORT) || 5432,
     username: process.env.DB_USER || 'postgres',
     password: process.env.DB_PASSWORD || 'postgres',
Enter fullscreen mode Exit fullscreen mode

Why this file matters

setup.ts is executed by Jest before any test suite runs (globalSetup in jest.config.js). It establishes a TypeORM connection that all integration tests share. By pulling DB_HOST from process.env, the same file now works across three contexts:

Context DB_HOST source
Local dev (Docker) docker-compose.yml service env var
CI (GitHub Actions) env: block in the workflow file
Legacy local (no Docker) Fallback to 'localhost'

Updating the Docker‑Compose stack

No changes were required in docker-compose.yml because the service already exported the variable:

services:
  api:
    environment:
      - DB_HOST=postgres
      - DB_PORT=5433
Enter fullscreen mode Exit fullscreen mode

The test runner now reads process.env.DB_HOST (value postgres) and process.env.DB_PORT (value 5433). The connection string becomes postgres://postgres:postgres@postgres:5433/..., matching the container network alias.

Content‑Automation side effect

The same commit also auto‑generated a changelog entry (content/2026/11/30/VS/changelog.md) to document the change:

## [2026‑11‑30] VS

### Changed
- **`apps/api/src/__tests__/setup.ts`** – Ajusté la lógica de lectura de la variable de entorno: ahora `DB_HOST` se obtiene de `process.env.DB_HOST`
Enter fullscreen mode Exit fullscreen mode

While this file lives in the content folder, it’s part of our build‑in‑public pipeline that publishes weekly updates to Medium, Substack, and Bluesky. The pipeline reads metadata.json flags (medium_generated, substack_generated) to decide which platforms to push to. By committing the changelog alongside the code fix, the automation script (scripts/publish.js) automatically includes the entry in the next newsletter without manual copy‑pasting.

Key Takeaway

Never hard‑code infrastructure values in test fixtures; always source them from process.env. This single line (const DB_HOST = process.env.DB_HOST ?? 'localhost';) made the test suite portable across Docker, CI, and legacy setups, eliminating environment drift and reducing future maintenance overhead.

What's Next

  1. Add a dedicated .env.test file that lists all required variables (DB_HOST, DB_PORT, etc.) and load it in CI via dotenv-cli.
  2. Extend the Jest global teardown to drop the test database after each run, ensuring a clean slate for the next CI job.
  3. Instrument the content‑automation pipeline to auto‑increment a build_number in metadata.json, so each newsletter includes the exact commit SHA that introduced the change.

Roberto Luna Osorio – Full Stack Developer & Project Lead

Playa del Carmen, México

vibecoding #buildinpublic #javascript #nodejs #docker #jest #typeorm


Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.

Repo: zaerohell/content-automation · 2026-12-01

#playadev #buildinpublic

Top comments (0)