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 6/1/2026
When a requested port is in use, the tests verify the fallback behavior. Here are the key test cases:
Single preferred port unavailable [test.js:L23-L30]:
When a desired port (8282) is already bound by a server, getPort() returns a different available port instead. The test verifies that:
- The returned port is a number greater than 0
- The returned port is not the unavailable desired port
Multiple preferred ports unavailable [test.js:L57-L75]:
When the first port in an array of preferred ports (9090) is occupied, getPort() tries the next one (9091). The test verifies:
- If the first port is unavailable, it falls back to the second port in the array
- If all ports in the array are unavailable, it returns a random available port that is:
- A valid port number (between 0 and 65,536)
- Not any of the unavailable desired ports
IPv4 host-specific test [test.js:L60-L67]:
When checking with a specific host (0.0.0.0), if the first preferred port is taken, the function still returns the next available port from the preferred list.
The core pattern is: the function gracefully degrades from preferred ports to a randomly allocated available port when the preferred ones are in use.
FOLLOW_UPS:
- How does the reserve option affect port locking behavior?
- What validation errors can the exclude parameter throw?
- How do iterables work as preferred port inputs?
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.