RepoPilot

What happens in tests when the requested port is in use? What's verified?

About sindresorhus/get-port · 2 messages · last activity 7/27/2026

Question

What happens in tests when the requested port is in use? What's verified?

RepoPilot

When the requested port is in use, the tests verify that getPort() falls back to an available port instead of returning the unavailable one.

For example, in the "preferred port unavailable" test [test.js:L23-L31], a server is first bound to port 8282, then getPort() is called requesting that same port:

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:

  1. The returned port is a number
  2. The port is valid (> 0)
  3. The returned port is NOT the desired port (t.not(port, desiredPort))

Similarly, the "all preferred ports in array are unavailable" test [test.js:L90-L104] binds servers to multiple ports and verifies that getPort() returns a different port altogether:

const port = await getPort({port: desiredPorts});
t.is(typeof port, 'number');
t.true(port > 0 && port < 65_536);
t.not(port, desiredPorts[0]);
t.not(port, desiredPorts[1]);

This confirms the fallback behavior works across multiple unavailable ports.

FOLLOW_UPS:

  • How does the locking mechanism prevent race conditions?
  • What does the reserve option do differently?
  • How are IPv4 and IPv6 hosts handled?

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.