Skip to content
Latchkey

NestJS microservices transport connect error in CI

A Nest microservice client or server could not reach its transport broker during a test, because the broker (Redis, RabbitMQ, Kafka, NATS) was not provisioned in the CI job.

What this error means

Microservice tests fail with a transport connection error like "connect ECONNREFUSED" for the broker port, or the client logs it cannot reach the message broker.

NestJS
[Nest] ERROR [ClientProxy] Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)

Common causes

No broker service container in the job

The transport client expects Redis or RabbitMQ on a port that nothing is listening on in CI.

The transport options point at the wrong host

The configured host or port does not match the service container the workflow started.

How to fix it

Provision the broker as a service

  1. Add the broker to the job services: with a health check.
  2. Point the transport options at that host and port via env.
  3. Wait for the broker to be healthy before running the tests.
.github/workflows/ci.yml
services:
  redis:
    image: redis:7
    ports: ['6379:6379']
    options: --health-cmd "redis-cli ping" --health-interval 5s --health-retries 5

Drive transport config from env

Read broker host and port from environment so CI can override them for the container.

app.module.ts
ClientsModule.register([{
  name: 'MATH_SERVICE',
  transport: Transport.REDIS,
  options: { host: process.env.REDIS_HOST ?? 'localhost', port: 6379 },
}])

How to prevent it

  • Provision every broker a microservice test needs as a service container.
  • Configure transport host and port from environment variables.
  • Add a readiness wait so tests do not race broker startup.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →