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 support #2183
Comments
|
Here is the reason why it is currently not working
I think this would be quite a big effort, the whole request interception logic would need to be redone. Without looking further into it, I think this sounds great. But we should split up A good first step would be to create a separate package as discussed in the thread you linked. Once that is working, we can incorporate it into nock. What do you think? |
|
I agree, we have 2 major fronts
Building the first helps the latter. |
|
that sounds good. We discussed the decomposability of nock a while ago, we want to do it, just didn't get to it yet. Independent of that work, the first step would be a separate module which intercepts requests at the |
|
For what it's worth, I think this would be quite useful. I was attempting to use True, cc: @mcollina |
|
It was suggested above that nock should attempt to intercept requests at the network level. An alternative proposal: nock can implement an undici undici sends all requests to the Could be as simple as: undici.setGlobalDispatcher(nock.wrapUndiciDispatcher(undici.getGlobalDispatcher()));If code decides to pass an explicit dispatcher into undici calls, they will bypass nock's wrapper. But I'm guessing the complexity of this approach is way less than attempting to proxy networking sockets and parse http headers as they pass through. The implementation might be built on top of undici's own mocking API, if it's sufficient. Benefit for end-users: |
|
I like it, but that might be a separate project. I started working on decomposing I hope to get back into it in August/September during my parental leave, but no promised ✌🏼 Once decomposed, the interceptor could be swapped out to one that supports undici, while still supporting the high-level nock APIs for mocking and assertions. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
We need to disable the new fetch implementation for now due to nock/nock#2183. Signed-off-by: Anders Kaseorg <anders@zulip.com>
|
Hi @gr2m and the nock community ! I was just wondering where we are regarding the ability to use nock to intercept undici and most importantly I suppose, fetch requests. Node 18 is moving to LTS this month so you can anticipate a pretty high number of users gradually moving away from node's historical Should you need any help in supporting this, just ask. nock is a great library that we use extensively, it'd be super cool to keep on using that with newer JS features ! Cheers, |
|
As a temporary work-around, for anyone coming here using node v18 and nock, I just disabled node.js fetch via |
BREAKING CHANGE: Dropping axios in favor of undici might break tests. For example, if using nock. See nock/nock#2183
BREAKING CHANGE: Dropping axios in favor of undici might break tests. For example, if using nock. See nock/nock#2183
BREAKING CHANGE: Dropping axios in favor of undici might break tests. For example, if using nock. See nock/nock#2183
|
A pro-tip I found when upgrading to node 18 is that you can define per-project In case someone finds this approach useful! |
|
What about having a setup flag (or even set as a default behaviour) for nock to polyfill Right now, the solution (thanks for everyone's input above!) isn't very elegant and will stop working somewhen in NodeJS 22+ I suppose, when the
{
"scripts": {
"test": "mocha --node-option no-experimental-fetch -r tests/_fetch-polyfill.ts tests/**/*.test.ts ...",
},
...
}
// nock doesn't support native fetch, and hence we need this polyfill.
import fetch, { Headers, Request, Response } from 'node-fetch';
if (!globalThis.fetch) {
(globalThis as any).fetch = fetch;
(globalThis as any).Headers = Headers;
(globalThis as any).Request = Request;
(globalThis as any).Response = Response;
} |
|
Undici has mock classes, maybe |
|
Actually I worked on this, it was not easy. But if we can use MockAgent, than we can also mock native fetch |
|
We have introduced experimental support for fetch. Please share your feedback with us. You can install it by: |
Context
NodeJS has a new vertical http client named undici which is much more performant.
Nock does not mock responses for undici users.
Alternatives
None at the moment.
Has the feature been requested before?
No results on undici search, nodejs/undici#531 has a small discussion on the mocking topic in the undici repo.
I wish to use the nock API to handle the mocking.
If the feature request is accepted, would you be willing to submit a PR?
Yes, pointer to what nock expects would be helpful.
The first step will be undici adding support for interceptors or moving the interception to a layer below (tcp level).
The text was updated successfully, but these errors were encountered: