Skip to content

MSW emits 2 error events when ClientRequest is destroyed #2617

@markuslewin

Description

@markuslewin

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 20 or higher

Node.js version

v22.21.0

Reproduction repository

https://github.com/markuslewin/msw-http-error

Reproduction steps

npm test

Current behavior

When ClientRequest.destroy is called with an error, 2 error events are emitted to the error listener. This results in an uncaught exception for HTTP clients that only expect 1 error, such as the clients in the Azure SDK for JavaScript.

If I disable MSW by removing the line server.listen(), the tests pass, but when MSW is enabled, I receive an uncaught exception for the second error event:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Uncaught Exception ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
AbortError: The operation was aborted. Rejecting from abort signal callback while making request.
 ❯ AbortSignal.<anonymous> node_modules/@typespec/ts-http-runtime/dist/esm/nodeHttpClient.js:199:36
 ❯ AbortSignal.[nodejs.internal.kHybridDispatch] node:internal/event_target:827:20
 ❯ AbortSignal.dispatchEvent node:internal/event_target:762:26
 ❯ runAbort node:internal/abort_controller:486:10
 ❯ abortSignal node:internal/abort_controller:457:3
 ❯ AbortController.abort node:internal/abort_controller:505:5
 ❯ Timeout._onTimeout node_modules/@typespec/ts-http-runtime/dist/esm/nodeHttpClient.js:85:33
 ❯ listOnTimeout node:internal/timers:588:17
 ❯ processTimers node:internal/timers:523:7

This error originated in "app.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
The latest test that might've caused the error is "azure credential doesn't throw". It might mean one of the following:
- The error was thrown, while Vitest was running this test.
- If the error occurred after the test had been completed, this was the last documented test before it was thrown.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Expected behavior

MSW should emit only 1 error event when ClientRequest.destroy is called with an error, mimicking the behavior of the original http.ClientRequest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds:triageIssues that have not been investigated yet.scope:nodeRelated to MSW running in Node

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions