Skip to content

fix: wait for ephemeral server to listen before dispatching request#890

Open
aguidrevitch wants to merge 1 commit into
forwardemail:masterfrom
aguidrevitch:master
Open

fix: wait for ephemeral server to listen before dispatching request#890
aguidrevitch wants to merge 1 commit into
forwardemail:masterfrom
aguidrevitch:master

Conversation

@aguidrevitch

@aguidrevitch aguidrevitch commented May 26, 2026

Copy link
Copy Markdown

Fix #255

When an app is passed to supertest, request(app) starts a temporary server with app.listen(0) and builds the URL from server.address().port. The port is instantly ready before the server emits 'listening', so if superagent dispatches in that window the request misses the app and returns a 404. That's why the route in #255 works from other clients but intermittently 404s under supertest. That's why starting a server outside of the supertest (with waiting for 'listening' event) also fixes the issue.

This PR waits for the server's 'listening' event before dispatching, but only when supertest started the server and it isn't listening yet. A regression test covers the race window, fails without fix, passes with the fix.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

Copilot AI review requested due to automatic review settings May 26, 2026 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Supertest throwing 404, Client says 200

1 participant