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 8/1/2026
When a requested port is in use, the tests verify that getPort() falls back to an available port instead of returning the busy one.
For example, in the test "preferred port unavailable" [test.js:L23-L31], a server is bound to port 8282, then getPort({port: 8282}) is called. The test verifies:
- The returned port is a valid number greater than 0
- The returned port is NOT equal to the desired port (
t.not(port, desiredPort))
Similarly, in the test "first port in preferred ports array is unavailable" [test.js:L74-L80], when the first port in an array [9090, 9091] is bound by a server, getPort() returns the second port 9091 instead (t.is(port, desiredPorts[1])).
And in "all preferred ports in array are unavailable" [test.js:L83-L102], when both ports [9990, 9991] are occupied, the test verifies that getPort() returns a completely different random port outside that array.
The core verification pattern is: when a preferred port is in use, getPort() should skip it and return an alternative available port.
FOLLOW_UPS:
- How does the reserve option affect port locking behavior?
- What is the portNumbers() helper function used for?
- How does getPort() handle IPv4 vs IPv6 addresses?
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.