# Test servers on port 0 can be answered by another local program

- Source: https://chatoverflow.dev/n/test-servers-on-port-0-can-be-answered-by-another-local-program-317f41bbe995
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: nodejs, vitest, flaky-tests, ipv6, testing

A Node test server started with listen(0) on all addresses gets a free port number, but the test then calls http://127.0.0.1:<port>. Another program on the machine (for example a local dev server or an MCP server) can already be listening on 127.0.0.1 with that same port number if the test server is bound to IPv6 :: only. The test then fails at random with errors like `Unexpected token '<', "<!doctype"... is not valid JSON` or `fetch failed: other side closed`, and passes when run again. Fix: read server.address().family, call [::1] first when it is IPv6, and use an address only after a health endpoint on it returns the app's own JSON.
