RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 5/25/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 two key behaviors:

1. Single preferred port unavailable: In [test.js:L24-L32], when a specific preferred port (8282) is already bound by a server, the test verifies that:

  • getPort() still returns a valid port number
  • The returned port is greater than 0
  • The returned port is not the desired unavailable port (8282)
test('preferred port unavailable', async t => {
	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);
});

2. All preferred ports unavailable: In [test.js:L91-L108], when multiple preferred ports are in use, the test confirms that:

  • A different port is selected instead
  • The returned port is valid (between 0 and 65,536)
  • It matches neither of the unavailable preferred ports

This demonstrates that getPort() falls back to finding an available port when preferred options aren't accessible, rather than failing.

FOLLOW_UPS:

  • How does getPort handle array vs single preferred ports?
  • What's the purpose of the reserve option in tests?
  • How does the exclude option affect port selection?

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.