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

Support Bun runtime #1718

Closed
4 tasks done
OleksandrKucherenko opened this issue Aug 29, 2023 · 3 comments
Closed
4 tasks done

Support Bun runtime #1718

OleksandrKucherenko opened this issue Aug 29, 2023 · 3 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

@OleksandrKucherenko
Copy link

OleksandrKucherenko commented Aug 29, 2023

Prerequisites

Environment check

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

Node.js version

v18.15.0

Reproduction repository

OleksandrKucherenko/chatgpt-translate-md#8

Reproduction steps

Refs:

Notes:
For switching between TS-NODE JEST and BUN you will need to changes lines:
image

this is YARN 3.xx monorepo with multiple packages, that I try to make compatible with BUN 0.8.1

Current behaviour

BUN produce this error

clis/gpt/src/gpt.test.ts:
879 |     if (this.aborted)
880 |       return;
881 |     this[kAbortController].abort();
882 |   }
883 | 
884 |   constructor(input, options, cb) {
                             ^
TypeError: Cannot call a class constructor without |new|
      at ClientRequest (node:http:884:26)
      at new NodeClientRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:104:20)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/http.request.js:37:15
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:284:8
      at new RedirectableRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:66:2)
      at request (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:523:13)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:438:10
      at dispatchHttpRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:149:54)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:143:4
      at new Promise (:1:20)
image

Expected behavior

YARN+TS-NONE - produce this error

 FAIL  clis/gpt/src/gpt.test.ts
  ● gpt › should raise custom error on API error 429

    ⛔ API error.

      117 |   // raise errors on non-2xx responses or empty content
      118 |   if (!(response.status >= 200 && response.status < 300))
    > 119 |     throw new Error(Errors.ApiError, { cause: createAxiosError(response) })
          |           ^
      120 |   if (content === ``) throw new Error(Errors.EmptyContent)
      121 |
      122 |   return content

      at askGPT (clis/gpt/src/gpt.ts:119:11)
      at Object.<anonymous> (clis/gpt/src/gpt.test.ts:66:20)

    Cause:
    AxiosError: Request failed with status code 429

      3 | /** stolen from: axios/lib/core/settle.js:17:12 */
      4 | export const createAxiosError = (response: AxiosResponse): AxiosError =>
    > 5 |   new AxiosError(
        |   ^
      6 |     `Request failed with status code ${response.status}`,
      7 |     [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
      8 |     response.config,

      at createAxiosError (clis/gpt/src/createAxiosError.ts:5:3)
      at askGPT (clis/gpt/src/gpt.ts:119:63)
      at Object.<anonymous> (clis/gpt/src/gpt.test.ts:66:20)
image
@OleksandrKucherenko OleksandrKucherenko added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Aug 29, 2023
@kettanaito kettanaito changed the title MSW/node in BUN runtime Support Bun runtime Aug 29, 2023
@kettanaito
Copy link
Member

I'm closing this because MSW functions without issues in Node.js. If there are any issues in Bun, those are likely Bun issues with Node.js compatibility. I highly recommend you file those in Bun's repo to make the team aware. If you find something MSW-specific, feel free to update this thread.

@Valdermeyder
Copy link

Regarding your response above. It can be hard to draw a line between who should support what. Bun is not 100% compatible with NodeJs and maybe it shouldn't be. In both cases it would be great to have msw works in bun

Anyway, just wanted to share with you another error.
I'm trying to use Bun just as a testing runtime and the issue I spotted today is msw is failing on

458 |     const { status, statusText, headers, body } = mockedResponse;
459 |     this.response.statusCode = status;
          ^
TypeError: Attempted to assign to read only property.
      at respondWith (/Users/volodymyrhuzar/Sources/Twill/twill-email-templates/node_modules/@mswjs/interceptors/lib/node/chunk-YXLNUCS2.mjs:459:5)
      at /Users/volodymyrhuzar/Sources/Twill/twill-email-templates/node_modules/@mswjs/interceptors/lib/node/chunk-YXLNUCS2.mjs:342:9

bun - 1.0.26
"msw": "2.2.0"
Let me know if you are interested in more details

@kettanaito
Copy link
Member

@VinceMalone, I can say with rather solid confidence that MSW will not ship anything specific to Bun. If you want to use MSW in Bun, you have to ensure Bun provides sufficient compatibility with Node.js (the http module and the global Fetch API, mainly).

Regarding the error, looks like a bug in Bun. The IncomingMessage.statusCode is not a readonly property. It's being assigned in the Interceptors library correctly.

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

3 participants