What happens in tests when the requested port is in use? What's verified?
What happens in tests when the requested port is in use? What's verified?
About sindresorhus/get-port · 2 messages · last activity 7/13/2026
When a requested port is in use, the tests verify that getPort() falls back to returning an alternative available port instead of the desired one.
For example, in the "preferred port unavailable" test [test.js:L23-L31], a server is first bound to port 8282, then getPort({port: 8282}) is called. The test verifies:
- A port number is returned (
t.is(typeof port, 'number')) - The port is valid and positive (
t.true(port > 0)) - Crucially, the returned port is NOT the desired port (
t.not(port, desiredPort))
Similarly, when all preferred ports are unavailable [test.js:L93-L108], two servers bind to ports 9990 and 9991, and getPort({port: [9990, 9991]}) is called. The test verifies that the returned port is a valid number in the proper range, but is neither 9990 nor 9991.
There's also a test for "first port in preferred ports array is unavailable" [test.js:L73-L80], where the first preferred port (9090) is bound by a server, and getPort({port: [9090, 9091]}) is expected to return the second port (9091) instead.
The core verification pattern is: when requested ports are unavailable, getPort() returns a different available port, not the unavailable one.
FOLLOW_UPS:
- How does the reserve option prevent port reuse?
- What does the clearLockedPorts function do?
- How does host parameter affect port availability checks?
Want to ask your own question?
Open chat for sindresorhus/get-portPublic Q&A. Generated by RepoPilot from the actual source of sindresorhus/get-port. AI answers can be incomplete or stale — verify before relying on them.