Discourage implicit use of ephemeral ports in supertest.serverAddress
Author: kwasimensahCreated Aug 10, 2020Updated Jul 13, 2026
I've been search for flakes in my test for weeks and I think the solution is around supertest's usage of ephemeral ports (binding to port 0).
Full writeup at at https://stackoverflow.com/questions/63343123/nodesupertest-flakes-with-client-network-socket-disconnected-before-secure-tls/63343124#63343124.
tldr; supertest.serverAddress will call app.listen(0) if the server isn't running yet. But the socket will have the SO_REUSEADDR flag set which means it can conflict with any other process that's also using SO_REUSEADDR on a port in the ephemeral range. This causes the client to spuriously fail at making a connection.
Source: forwardemail/supertest