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/6/2026
When a requested port is in use, the tests verify that getPort() falls back to finding an available port. Here's what's tested:
In the "preferred port unavailable" test [test.js:L23-L31], when port 8282 is already bound by a server:
const desiredPort = 8282;
const server = net.createServer();
await promisify(server.listen.bind(server))(desiredPort);
const port = await getPort({port: desiredPort});
t.is(typeof port, 'number');
t.true(port > 0);
t.not(port, desiredPort);
The test verifies three things:
- A port number is returned (type
number) - The returned port is valid (
> 0) - The returned port is NOT the unavailable desired port (
t.not(port, desiredPort))
There's also a more complex test called "first port in preferred ports array is unavailable" [test.js:L75-L80] which tests the same behavior when the first port in an array of preferred ports is occupied—it should skip it and try the next available one.
This aligns with the README's promise that when a preferred port is unavailable, getPort will "fall back to a random port" [readme.md:L17-L19].
FOLLOW_UPS:
- How does the reserve option change port locking behavior?
- What happens with multiple concurrent getPort calls?
- How are IPv4 and IPv6 hosts handled differently?
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.