RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 9/1/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() falls back to returning a different available port instead of the requested one.

For example, in the test "preferred port unavailable" [test.js:L23-L31], a server is first bound to port 8282. Then getPort({port: 8282}) is called, and the test verifies:

  1. A port number is returned (t.is(typeof port, 'number'))
  2. The port is valid (t.true(port > 0))
  3. The returned port is not the requested one (t.not(port, desiredPort))

Similarly, the test "all preferred ports in array are unavailable" [test.js:L99-L116] binds servers to multiple ports (9990 and 9991), then calls getPort({port: [9990, 9991]}) and verifies that a different valid port is returned that matches neither of the unavailable preferred ports.

Another test, "first port in preferred ports array is unavailable" [test.js:L82-L95], binds only the first port in a preferred array, then verifies that getPort() returns the second port from the array instead.

The key pattern: when preferred ports are unavailable, the library skips them and either tries the next port in the array or falls back to a randomly assigned available port.

FOLLOW_UPS:

  • How does the reserve option affect port availability?
  • What is the default lock duration for returned ports?
  • Can you exclude specific ports from consideration?

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.