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 5/18/2026
When the requested port is in use, the tests verify that getPort falls back to finding an available port instead of returning the unavailable one.
In the test "preferred port unavailable" [test.js:L23-L30], a server is created and bound to port 8282, then getPort is called requesting that same port. The test verifies three things:
- A port is returned -
t.is(typeof port, 'number') - The port is valid -
t.true(port > 0) - The port is not the requested one -
t.not(port, desiredPort)
Similarly, in the test "first port in preferred ports array is unavailable" [test.js:L72-L80], a server occupies the first port in an array of preferred ports [9090, 9091], and the test verifies that getPort returns a different port from the array instead.
This behavior demonstrates that getPort gracefully handles port conflicts by automatically finding an alternative available port rather than failing.
FOLLOW_UPS:
- How does the reserve option affect port locking behavior?
- What is the default time window for port reuse?
- How does the exclude option work with port selection?
Want to ask your own question?
Open chat for sindresorhus/get-port →Public 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.