mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

RangeError: init["status"] must be in the range of 200 to 599, inclusive (Undici, Jest) #2307

Closed Acetyld closed 6 days ago

Acetyld commented 1 month ago

Prerequisites

Environment check

Node.js version

v22.3.0

Reproduction repository

https://github.com/stichingsd-vitrion/msw_jest_undici_reproduction

Reproduction steps

  1. Clone project
  2. Run "yarn"
  3. Run "yarn test" once or twice

Current behavior

      at src/dummy.test.ts:4:33

 PASS  src/dummy.test.ts
  โœ“ msw (124 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.266 s, estimated 2 s
Ran all test suites.
node:internal/deps/undici/undici:9037
        throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.');
        ^

RangeError: init["status"] must be in the range of 200 to 599, inclusive.
    at initializeResponse (node:internal/deps/undici/undici:9037:15)
    at new Response (node:internal/deps/undici/undici:8829:9)
    at MockHttpSocket.onResponseStart (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:547:22)
    at Object.read (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:80:31)
    at MockHttpSocket.push (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts:56:18)
    at Socket.<anonymous> (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:247:14)
    at Socket.emit (node:events:520:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Socket.Readable.push (node:internal/streams/readable:390:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:191:23)

Node.js v22.3.0
error Command failed with exit code 1.

Expected behavior

No errors recarding Undici

kettanaito commented 1 month ago

Hi, @Acetyld. Thanks for reporting this.

I looked at the repo briefly, your usage seems to be okay. This may be a bug in MSW. I will explore this in more detail once I have a moment.

The error suggests that the underlying socket instance starts receiving a response whose status code is a non-configurable range (200 - 599):

/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:547:22

You can log out that response header received on that line (just go to the build *.js/*.mjs file, not the *.ts) and see what kind of response is that, and whether it gives you any hints why your test receives that response.

I'm quite curious what kind of response that may be.

Acetyld commented 1 month ago

Cool, i tried looking int he builded js/mjs files but couldt reference it my self.

Acetyld commented 1 month ago

Hi @kettanaito it all happens because of the websocket stuff image

I get a 201. Can i just disable the websocket stuff or something?

kettanaito commented 1 month ago

Are you using WebSockets in tests? I wonder what triggers 101, to begin with. That has to be your tested code or your test environment.

Acetyld commented 1 month ago

Are you using WebSockets in tests? I wonder what triggers 101, to begin with. That has to be your tested code or your test environment.

Yhea i created a full reproduction see https://github.com/stichingsd-vitrion/msw_jest_undici_reproduction

I do nothing special in my opinion. Do you also get it with repro? Would love to hear =)

Acetyld commented 4 weeks ago
diff --git a/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js b/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
index 648938e..85e8a13 100644
--- a/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
+++ b/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
@@ -348,6 +348,7 @@ var MockHttpSocket = class extends MockSocket {
         this.responseStream = new (0, _stream.Readable)({ read() {
         } });
       }
+      let newStatus = status > 200 ? status : 200
       const response = new Response(
         /**
          * @note The Fetch API response instance exposed to the consumer
@@ -358,7 +359,7 @@ var MockHttpSocket = class extends MockSocket {
          */
         canHaveBody ? _stream.Readable.toWeb(this.responseStream) : null,
         {
-          status,
+          status:newStatus,
           statusText,
           headers
         }
diff --git a/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs b/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
index c6d2383..33a8411 100644
--- a/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
+++ b/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
@@ -348,6 +348,8 @@ var MockHttpSocket = class extends MockSocket {
         this.responseStream = new Readable({ read() {
         } });
       }
+      let newStatus = status > 200 ? status : 200;
+
       const response = new Response(
         /**
          * @note The Fetch API response instance exposed to the consumer
@@ -358,7 +360,7 @@ var MockHttpSocket = class extends MockSocket {
          */
         canHaveBody ? Readable.toWeb(this.responseStream) : null,
         {
-          status,
+          status:newStatus,
           statusText,
           headers
         }

For now ill use this as a patch-package

wslp12 commented 2 weeks ago

same issue problem = console-ninja extenstion vscode console ninja using socket

tsteckenborn commented 1 week ago

Yeah, or e.g. Effect Devtools using socket

kettanaito commented 6 days ago

@Acetyld, thanks for publishing a reproduction repo! That package patch though, I don't recommend doing that. You are disrupting the data transfer, and responses like 101 won't be received and handled properly by Node.js. Do that at your own risk.

I will take a look at your repo once I have a minute and track down the root cause for this.

From the looks of it, this brings us back to non-configurable response status codes. We do something around this already in Interceptors, perhaps just not in MockHttpSocket.

@wslp12 @tsteckenborn thanks for confirming this happens during WebSocket usage!

Acetyld commented 6 days ago

Yup! I also have Console Ninja, so @wslp12 thanks for pointing that out ๐Ÿ˜‰

kettanaito commented 6 days ago

The fix is written in https://github.com/mswjs/interceptors/pull/677.

kettanaito commented 6 days ago

Released: v2.6.5 ๐ŸŽ‰

This has been released in v2.6.5!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.