Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of AbortSignal causes "Cannot construct a Request with a Request object that has already been used." error #1924

Open
4 tasks done
ezzatron opened this issue Dec 12, 2023 · 2 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@ezzatron
Copy link
Contributor

Prerequisites

Environment check

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

Node.js version

v21.4.0

Reproduction repository

https://github.com/ezzatron/msw-abort-signal-repro

Reproduction steps

  1. npm ci
  2. npm test

Current behavior

Instead of producing an abort error, the repro code produces the following error, despite only a single request ever being made:

Cannot construct a Request with a Request object that has already been used.

See example output: https://github.com/ezzatron/msw-abort-signal-repro/actions/runs/7177213214/job/19543363576#step:5:25

Expected behavior

The reproduction code should produce an abort error consistent with use of an AbortSignal.

@ezzatron ezzatron added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Dec 12, 2023
@liorpevzner
Copy link

You are using the controller.abort() before the fetch, the controller is already used, you can't reuse it after its been aborted.
You need to switch the order, remove the await on the fetch request and put the controller.abort() right after the fetch, it should be caught in the .catch(error).

@ezzatron
Copy link
Contributor Author

You are using the controller.abort() before the fetch, the controller is already used, you can't reuse it after its been aborted.

I appreciate the reply, but I don't agree. An AbortSignal is not guaranteed to be aborted only after it is used in a fetch call, and the abort signal is not always created by the same code that uses it. It should not be causing MSW to change behaviour here.

As an example, fetch handles this just fine: https://codepen.io/ezzatron/pen/xxBEevM?editors=0010

To clarify, MSW should still be throwing an exception in this case. But it should be an abort error, not a generic error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

2 participants