RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 6/29/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 finding an alternative available port. Here's what's tested:

Single unavailable preferred port [test.js:L23-L30]: When port 8282 is already bound by a server, calling getPort({port: 8282}) returns a different port number. The test verifies:

  • The returned value is a number
  • It's greater than 0
  • It is not equal to the desired port (8282)

Multiple unavailable preferred ports [test.js:L81-L97]: When all ports in an array (9990, 9991) are already bound, getPort({port: [9990, 9991]}) returns a random available port. The test verifies:

  • The returned value is a number between 0 and 65,536
  • It doesn't match the first preferred port (9990)
  • It doesn't match the second preferred port (9991)

Partially unavailable array [test.js:L72-L80]: When the first port in an array [9090, 9091] is unavailable but the second isn't, getPort() returns the second port in the array, demonstrating it tries alternatives in order before falling back to a random port.

The key verification pattern: the function doesn't fail or throw an error—it gracefully returns an alternative available port instead.

FOLLOW_UPS:

  • How does get-port check if a port is available?
  • What's the difference between reserve option and default locking?
  • How does the exclude option work in practice?

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.