Skip to content
Latchkey

Firebase Auth emulator "ECONNREFUSED" in CI

The test suite tried to reach the Firebase Auth emulator on 127.0.0.1:9099 but the connection was refused. The emulator was not yet listening, or it bound a different host or port.

What this error means

Auth tests fail with "Error: connect ECONNREFUSED 127.0.0.1:9099". The emulator was started but tests ran before it was ready, or the configured port does not match.

Firebase emulator
Error: connect ECONNREFUSED 127.0.0.1:9099
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)

Common causes

Tests ran before the emulator was ready

The emulator takes a moment to bind its port; tests started too early get connection refused.

A port or host mismatch

The emulator bound a different port than the env expects, so the client connects nowhere.

How to fix it

Run tests via emulators:exec

  1. Wrap the test command in firebase emulators:exec so it waits for readiness.
  2. Set the emulator host env to match the started port.
  3. Do not launch tests in parallel with the emulator start.
Terminal
firebase emulators:exec --only auth "npm test"

Match the configured emulator port

Align firebase.json emulator ports with FIREBASE_AUTH_EMULATOR_HOST so the client hits the right port.

How to prevent it

  • Use emulators:exec so tests wait for the emulator.
  • Keep firebase.json ports and the host env in sync.
  • Avoid racing test start against emulator start.

Related guides

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