msw
msw copied to clipboard
Use of AbortSignal causes "Cannot construct a Request with a Request object that has already been used." error
Prerequisites
- [X] I confirm my issue is not in the opened issues
- [X] I confirm the Frequently Asked Questions didn't contain the answer to my issue
Environment check
- [X] I'm using the latest
msw
version - [X] 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
-
npm ci
-
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
.
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).
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.
so,any iead to resolve this problem?
I'm facing this issue too. Would love to see the needle move on fixing this.