// Test helper: start a throwaway Express app on an ephemeral port and return its // base URL + a close(). Uses the built-in fetch (Node 18+) so tests need no // extra HTTP dependency. Tests here exercise middleware in isolation and do NOT // touch the database. const express = require('express') async function startApp(configure) { const app = express() app.use(express.json()) configure(app) const server = await new Promise((resolve) => { const s = app.listen(0, '127.0.0.1', () => resolve(s)) }) const { port } = server.address() return { url: `http://127.0.0.1:${port}`, // `server.close()` stops accepting and waits for open connections to end on // their own — and node's global fetch keeps its sockets alive, so nothing // ever ends them. The listener then outlives the test that made it, which // used to be invisible because the pool held the process open anyway. close: () => new Promise((resolve) => { server.closeAllConnections() server.close(resolve) }), } } module.exports = { startApp }