-
-
Notifications
You must be signed in to change notification settings - Fork 132
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 Undici #159
Comments
Hey, @tompeace. Thank you for suggesting this. I've head about undici and I think we should give its interceptor a try. Do you have a confirmation that using
You can take a look at the existing interception tests for inspiration. Once we have the test we can move forward with undici support. Let me know if you have any difficulty writing that test. Thank you! |
Hi @kettanaito @tompeace, I'm not sure I'm doing everything right, but It might be a good starting point to work on the undici support. |
Hey, @chentsulin! Thank you for your work on that. I'll take a look at the pull request as soon as I have time. I'm sure it'll act as a great foundation to introduce the Undici support itself. |
Has anyone made any headway on this interceptor? |
Hey, @SupremeTechnopriest. No, there hasn't been any progress on this. We've started by adding some failing tests in #180 but the implementation is still missing. Would you be interested in working on this? I'd be happy to support you with code reviews and discussions about bringing Undici support to this library. |
Thinking I will take a swing at it. Has the tests been merged? Not seeing them on the main branch. |
No worries, I just pulled the tests into my fork. I Imagine you want to try to intercept as close to the core of undici as possible. Maybe the connection from the |
@SupremeTechnopriest sounds like a good starting point. I've noticed that Undici uses a mocked Overall, we should really aim at agnostic implementation, meaning there should be 0 things relevant to Undici in how we intercept requests. Currently, we intercept |
Here is where I'm looking: https://github.com/nodejs/undici/blob/main/lib/core/connect.js It would be nice to get our interceptor in between |
I think A fair warning that I'm still quite new to how network requests work in Node.js so I may be saying things that are not true. This is a good opportunity for me to learn more. |
I found the same. This would be the next level up https://github.com/nodejs/undici/blob/main/lib/core/request.js but we are venturing toward an interceptor specific to undici. |
That seems like a custom |
Additionally, regarding supporting native Node.js 17+ fetch, unfortunately — |
Took me awhile to figure out why my requests were not being mocked when I upgraded a third-party package! As a workaround, I've used |
This will become a more urgent issue as Node 18 moves to active status in October. In Node 18, native fetch is enabled by default using an undici implementation. At the moment msw cannot intercept undici requests. (In fact, my attempt to upgrade to node 18 broke even interception of As mentioned, undici has its own mocking API. I want to upgrade to Node 18 to get the stable native fetch that is spec-compliant so I can use stream readers. But I also want to keep using msw. It's an early codebase though, so it wouldn't be hard to migrate existing tests to use undici mock interceptors, which could increase compatibility in my primary testing environment (vitets + jsdom). But then I'd be losing the wonderful msw API and support for in-browser testing with service workers. FWIW, re: @varanauskas comment from a few months ago, import { ProxyAgent, setGlobalDispatcher } from "undici";
setGlobalDispatcher(new ProxyAgent("http://127.0.0.1:8080")); |
Node 18 beomes active LTS in October, and is desirable because it includes a native `fetch` implementation and support for web streams, meaning that `fetch` should work the same in browser and node for almost all functionality (this all started when looking for `body.getReader()`). Unfortunately this breaks a bunch of stuff, like all our tests using msw to mock HTTP API, since msw can not currently intercept requests from Undici, see: mswjs/interceptors#159 Currently looking for best solution to this
Ideas and approaches on how to intercept Undici are welcome. But, I'd rather focus on native I know Undici has their own mocking setup but it's too high-level to be integrated in this library. The native fetch, however, despite being powered by Undici, should be intercepted without issues as we currently patch the global
Intercepting Undici as a standalone solution is tricky because I have a strict policy of not introducing third-party library-specific implementations. But, as I see Undici gaining adoption in Node natively, perhaps we can skip this step altogether.
If you'd like to contribute, here's a roadmap:
|
Hey @kettanaito thanks for the reply! I actually started working on this yesterday and came to mostly the same conclusions. I realized that the reason msw wasn't intercepting native fetch out of the box was because the fetch interceptor is not included in the node preset. I started adding some tests for this (basically copying the fetch tests from browser), and it seems promising so far, but I've gotten distracted by tangentially related issues with IPv6 URL serialization (#282 ) Hopefully will have a more meaningful update / some code to share soon |
Oh, that's an interesting find! Yeah, since we haven't considered global fetch in Node when writing the I'm going to look at that issue you've linked to unblock you in your research. Superb work, by the way! Would love to see your findings as a pull request 👀 |
Undici updateWe will not support Undici directly. It's a third-party module and we have a strict policy of implementing interception to be library-agnostic. That being said, we will support global fetch in Node, which is implemented by Undici and is available since Node 17. We will add the How you can helpPlease give the import { FetchInterceptor } from '@mswjs/interceptors/lib/interceptors/fetch'
const interceptor = new FetchInterceptor()
interceptor.apply()
interceptor.on('request', handler)
|
@kettanaito That does look promising. Would there be a way to beta test this out with I did have to update the import in your example: - import { FetchInterceptor } from '@mswjs/interceptors/lib/interceptors/FetchInterceptor'
+ import { FetchInterceptor } from "@mswjs/interceptors/lib/interceptors/fetch"; When we tried out the following code in Node.js it worked as expected import { FetchInterceptor } from "@mswjs/interceptors/lib/interceptors/fetch";
const interceptor = new FetchInterceptor();
interceptor.apply();
interceptor.on("request", (request) => {
console.log(request.method); // Logs `GET`
console.log(request.url); // Logs `http://localhost:8080/api/ips/current?viewAs=&ip=`
}); |
We can add the Fetch interceptor to the Node preset in MSW and release this feature. |
Opened global fetch support in MSW: mswjs/msw#1543 |
undici is a node.js library written by some core node.js contributors. It offers many great features for making fast http request that largely outperform the native
http
&https
modules by using a customer http parser calledllhttp
.MSW does not work with this library as it does not mock any of the modules covered in
./src/interceptors/ClientRequest
.undici does offer its own mocking mechanism, but I love MSW's api and am wondering if there are any plans to add an interceptor for this in the future?
For context my use case is mocking graphql
apollo-datasource-http
requests.Thanks!
The text was updated successfully, but these errors were encountered: