-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 msw
#2297
Comments
Thank you for reporting this. Our I'm currently working on porting all of the |
Sorry to bother, do we have trouble in porting node:http? |
@Jarred-Sumner sorry to bother, can you have a look at this problem, which broke |
Current output in bun: ➜ bun index.ts
🚀 ~ http: {
Agent: [class Agent],
Server: [class Server],
createServer: [Function],
ServerResponse: [class ServerResponse],
IncomingMessage: [class IncomingMessage],
request: [Function],
get: [Function],
maxHeaderSize: 16384,
validateHeaderName: [Function],
validateHeaderValue: [Function],
setMaxIdleHTTPParsers: [Function: setMaxIdleHTTPParsers],
globalAgent: { /* Removed for comparison to Node */ },
ClientRequest: [class ClientRequest],
OutgoingMessage: [class OutgoingMessage]
} Node is here {
_connectionListener: [Function: connectionListener],
Agent: [Function: Agent] { defaultMaxSockets: Infinity },
ClientRequest: [Function: ClientRequest],
IncomingMessage: [Function: IncomingMessage],
OutgoingMessage: [Function: OutgoingMessage],
Server: [Function: Server],
ServerResponse: [Function: ServerResponse],
createServer: [Function: createServer],
validateHeaderName: [Function: __node_internal_],
validateHeaderValue: [Function: __node_internal_],
get: [Function: get],
request: [Function: request],
maxHeaderSize: [Getter],
globalAgent: [Getter/Setter]
} So the discrepancy is now that only Bun has: |
The ticket here is specifically about |
Original issue has been fixed, but |
Issue in MSW repo: |
Some additional information about node.http module compatibility in BUN mswjs/interceptors#418 (comment) |
msw
Hey, folks. So from the report in mswjs/interceptors#418 (comment), it appears that this is no longer an issue with MSW 2.0. I highly encourage you updating and letting me know whether the same |
Hey, I try with a minimal repo like this import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node'
const handlers = [
http.get("https://bun.sh", () => {
return HttpResponse.json({ "test": "a" })
})
]
export const server = setupServer(...handlers)
server.listen({
onUnhandledRequest: "warn"
})
const response = await fetch("https://bun.sh");
const html = await response.text(); // HTML string And give this error:
The undefined is not a function is a console log of the error in the The error is inside the bun implementation of If I comment that function the library works like a charm |
Thanks to @Nisgrak's tips
Here is the workaround(currently in Bun v1.0.18): import { expect, test, describe, mock } from "bun:test"
// fix
mock.module("events", () => {
return {
setMaxListeners: () => {},
}
})
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node'
const handlers = [
http.get("https://bun.sh", () => {
return HttpResponse.json({ "test": "a" })
})
]
export const server = setupServer(...handlers)
server.listen({
onUnhandledRequest: "warn"
})
const response = await fetch("https://bun.sh");
const html = await response.text(); // HTML string |
Does it only work with import axios from "axios";
import { HttpResponse, http } from "msw";
import { setupServer } from "msw/node";
const axiosInstance = axios.create();
const handlers = [
http.get("https://bun.sh", () => {
return HttpResponse.json({ test: "a" });
}),
];
export const server = setupServer(...handlers);
server.listen({
onUnhandledRequest: "warn",
});
// this works fine
// const response = await fetch("https://bun.sh");
// const html = response.json();
// but with axios, it doesn't
const response = await axiosInstance.get("https://bun.sh");
const html = response.data; bun test v1.0.26 (c75e768a)
src/Sample.test.ts:
432 | total: totalResponseBodyLength
433 | });
434 | };
435 | if (response.body) {
436 | this.logger.info("mocked response has body, streaming...");
437 | const reader = response.body.getReader();
^
TypeError: response.body.getReader is not a function. (In 'response.body.getReader()', 'response.body.getReader' is undefined)
at respondWith (/Users/joeldaros/corteva/msw-2-issue/node_modules/@mswjs/interceptors/lib/node/chunk-IZG42TWK.mjs:437:22) |
I too facing the same issue. @joel-daros were you able to fix this issue? |
Nope. Maybe @kettanaito can help to identify if it’s a MSW or Bun issue. |
Adding below snippet in jest configuration worked for me.
|
@joel-daros, I can help indeed, with this issue any other issues: MSW works without problems in Node.js. Every exception you encounter while using it with Bun is Bun's issue related to Node.js compatibility (it can also be your misconfigured environment, like @kishores05 has pointed out, so first ensure MSW works in your environment). |
Is there any progress on this? |
A heads-up on this, in the near future MSW will migrate to a new architecture for request interception in Node.js (see mswjs/interceptors#515). That architecture will rely on
|
Does this still reproduce for you? using Bun 1.1.8 this works for me. |
yeah, still reproduce for me. did you try with the code demo i provide? update: with msw v2 lastest version(2.3.0), the code demo works, however, if i put a real handler, still has issues: import axios from 'axios';
import { http, passthrough, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
const server = setupServer(...[
http.get('https://swapi.dev/api/people/', () => {
// return passthrough()
return HttpResponse.json({ results: [{}, {}] })
}),
])
server.listen({
onUnhandledRequest: 'warn',
});
axios.get('https://swapi.dev/api/people/?page=2')
.then(function (response) {
// handle success
console.log(response.data.results.length);
})
.catch(function (error) {
// handle error
console.log(error?.message);
}); with output:
|
Looks like msw 2.4.4 is now using the |
looks like #13072 is already open |
Can confirm we've migrated to use built-in HTTP parser in Node.js since v2.4.4. If you wish to run MSW in Bun, Bun has to implement that parser. It may be tricky since it's technically an internal built-in |
What is the problem this feature would solve?
use
msw
in bun has some problem, after some exploration, findnode:http
is not fully implemented in Bun.with this code
and dependencies:
output in Node:
output in Bun
and first two of error stack is
What is the feature you are proposing to solve the problem?
fully implement
node:http
to support mswWhat alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: