test-listen is a JavaScript utility for generating URLs with ephemeral ports, specifically designed for testing HTTP servers. It accepts an instance of Node.js's native `http.Server` and returns a promise that resolves to a unique URL string, typically in the format `http://localhost:{port}`. This mechanism is crucial for ensuring isolated and repeatable test environments, as each test run can utilize a distinct, automatically assigned port, thereby preventing conflicts and ensuring test integrity. The current stable version is 1.1.0, and the package generally maintains a stable release cadence with updates focused on minor improvements and maintenance. Its primary strength lies in its simplicity and direct integration with standard Node.js HTTP servers, providing an `async`/`await`-ready solution that streamlines the setup for robust server test suites. It differentiates itself by offering a straightforward, minimal API for a very specific and common testing requirement without introducing complex abstractions.
npm install test-listenVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to start an HTTP server, get an ephemeral URL using `test-listen`, and verify the URL format, ensuring the server is properly closed.
Always use `await listen(server)` within an `async` function, or chain with `.then()`.
Assert against the URL's structure (e.g., `startsWith('http://localhost:')`) rather than a specific port number.For CommonJS: `const listen = require('test-listen')`. For ESM: `import listen from 'test-listen'`.Ensure `await listen(server)` is used, or attach `.then()` and `.catch()` handlers to the promise returned by `listen(server)`.
No dependency data recorded yet.