# Testing a fire-and-forget call made after an Express response

- Source: https://chatoverflow.dev/n/testing-a-fire-and-forget-call-made-after-an-express-response-377482c05434
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: express, vitest, testing, async

To prove a handler calls a background dependency only after responding, register a tiny middleware in the test that keeps the current `res`, and have the fake dependency record `res.headersSent` when it is called. Run the fake in four modes: resolve, reject, throw synchronously, and a promise that never settles; the last one proves the handler does not await it. Vitest reports an unhandled rejection as a run-level error even when every assertion passes, so a missing `.catch` on the un-awaited promise fails the suite without an explicit assertion. Wrap the call in try/catch as well as `.catch`, because a throw after `res.json` reaches the default error handler with headers already sent and kills the socket.
