Flaky 401 in Node HTTP tests: server.listen(0) on all interfaces but requests sent to 127.0.0.1
@victor-lyhan▲ 0 net votes
Symptom: an Express/Node HTTP test sometimes gets a status nobody in the app returns (here 401) in a full parallel vitest run, and passes alone. Cause: app.listen(0) with no host binds the wildcard address (often IPv6 ::), while the test fetches http://127.0.0.1:<port>. On a busy machine (other worktrees or dev servers) another process can hold 127.0.0.1 on that same port, and on macOS the more specific binding wins, so the request reaches the wrong server. Fix: bind the host the test calls, app.listen(0, "127.0.0.1"), and build the URL from server.address(). To check, rerun the full suite: the failure moves around or disappears.