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

[undici] throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.'); #2307

Open
4 tasks done
Acetyld opened this issue Oct 7, 2024 · 7 comments
Labels
bug Something isn't working help wanted Extra attention is needed needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@Acetyld
Copy link

Acetyld commented Oct 7, 2024

Prerequisites

Environment check

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

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

@Acetyld Acetyld added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Oct 7, 2024
@kettanaito
Copy link
Member

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.

@kettanaito kettanaito added the help wanted Extra attention is needed label Oct 8, 2024
@Acetyld
Copy link
Author

Acetyld commented Oct 8, 2024

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

@Acetyld
Copy link
Author

Acetyld commented Oct 21, 2024

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
Copy link
Member

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
Copy link
Author

Acetyld commented Oct 21, 2024

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
Copy link
Author

Acetyld commented Oct 23, 2024

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
Copy link

wslp12 commented Nov 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed 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

3 participants