RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 6/15/2026

Question

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

RepoPilot

When a requested port is in use, the tests verify that getPort returns an alternative port instead of the desired one.

In the test "preferred port unavailable" [test.js:L23-L31], a server is first bound to the desired port (8282), and then getPort({port: desiredPort}) is called. The test verifies:

  1. The returned port is a number: t.is(typeof port, 'number')
  2. The returned port is valid (greater than 0): t.true(port > 0)
  3. The returned port is not the desired port: t.not(port, desiredPort)

This confirms that when a preferred port is unavailable, getPort falls back to finding and returning a different available port rather than failing or returning the occupied port.

Similarly, the test "first port in preferred ports array is unavailable" [test.js:L63-L68] demonstrates the behavior when using multiple preferred ports—it binds a server to the first preferred port and then calls getPort with an array of ports, implicitly verifying that the function skips the occupied port and tries alternatives.

FOLLOW_UPS:

  • How does the reserve option prevent port reuse?
  • What does the exclude option do in getPort?
  • How are IPv4 and IPv6 ports handled differently?

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.