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
- Add the broker to the job
services:with a health check. - Point the transport options at that host and port via env.
- 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 5Drive 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
NestJS "TypeOrmModule ... connection ECONNREFUSED" in CIFix NestJS TypeOrmModule "connect ECONNREFUSED" in CI - the database service is not reachable at bootstrap be…
NestJS "Nest application failed to start" in CIFix "Error: Nest application failed to start" in CI - a lifecycle hook, module init, or async provider threw…
NestJS "EADDRINUSE: address already in use" in e2e in CIFix NestJS "listen EADDRINUSE: address already in use :::3000" in e2e in CI - a previous test app was not clo…