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

Mocking support #531

Closed
mcollina opened this issue Jan 27, 2021 · 17 comments
Closed

Mocking support #531

mcollina opened this issue Jan 27, 2021 · 17 comments
Labels
Status: in-progress This issue/pr is currently being worked on

Comments

@mcollina
Copy link
Member

I think it would be awesome if we had mocking support, similar to nock.

@adamovittorio
Copy link

@mcollina what can we do to help here, do you have an idea on how to do the magic? In my understanding undici is not using nodejs HTTP module under the hood, so we need to intercept the request in another way.

@mcollina
Copy link
Member Author

mcollina commented Feb 3, 2021

Send a PR :). I would recommend to add a MockedClient and then some support for mocking inside Pool, or maybe a MockingPool, and the rest will follow.

@adamovittorio
Copy link

What do you mean by adding a MockedClient? Just implementing a mocked version of the undici client?

In my understanding, we want something that intercepts the undici network calls and returns a mocked response.

const { MockClient } = require("undici");
const client = new MockClient("https://www.nodejs.org");

client.intercept({
  path: "/",
  method: "GET",
}).reply(200, { "foo": "bar"});

Wherever in you code you use undici to perform a GET https://www.nodejs.org/ the result is { "foo": "bar" } 200.

Still, I don't know how to intercept the requests

@mcollina
Copy link
Member Author

mcollina commented Feb 4, 2021

The API you are sketching is correct!

Still, I don't know how to intercept the requests

Neither would I! You might have to overload a few (or all) of the APIs in Client.

@piranna
Copy link

piranna commented Feb 23, 2021

You might have to overload a few (or all) of the APIs in Client.

According to docs, it would be a matter of overwrite just only client.dispatch().

@jonnydgreen
Copy link
Contributor

jonnydgreen commented Mar 4, 2021

I'm very happy to work on an implementation based on @adamovittorio's suggestion if that's okay with everyone? If so, it'll include the MockClient definition, tests, docs and typings :)

@mcollina
Copy link
Member Author

mcollina commented Mar 4, 2021

Go for it @jonnydgreen! Open a draft PR as early as possible so when can provide iterative feedback.

@jonnydgreen
Copy link
Contributor

jonnydgreen commented Mar 4, 2021

Awesome! Will do, I'll submit a draft PR for initial feedback as soon as possible! :)

@Nytelife26
Copy link
Contributor

If I may add, would it not be better to put the mocking in a separate package? Many users will not be using the mock functionality, and it is not integral to undici's function, so perhaps it should be put in an undici-mocks (or similar) package?

In regards to undici not using HTTP under the hood, I believe intercepting the requests at the net level is the better option since that would mock all Node network operations - both simultaneously removing the need for Nock and other individualized wrappers for various libraries.

@piranna
Copy link

piranna commented Mar 22, 2021

+1 to the independent package. Regarding mocking net built-in, it's interesting but too much low-level, we would not be able to intercept at HTTP layer... we would already need a module built on top of that net mock to do it.

@Nytelife26
Copy link
Contributor

@piranna Gotcha, it was more of a concept inspired by the old net-interceptor package.

Thank you for the +1 though. It seems like the dev team are keen to get it released first and then look into separating the packages later on, so hopefully, this will be done soon.

@Ethan-Arrowood Ethan-Arrowood added the Status: in-progress This issue/pr is currently being worked on label Apr 1, 2021
@gr2m
Copy link

gr2m commented Apr 8, 2021

similar to nock

Hey Matteo, I'm helping maintain nock. Can you tell why requests with undici cannot be mocked with nock at this point? It works with all other request libraries, as far as I know?

@piranna
Copy link

piranna commented Apr 8, 2021

Can you tell why requests with undici cannot be mocked with nock at this point? It works with all other request libraries, as far as I know?

AFAIK nock intercept requests at Node.js built-in http module, while undici is built from scratch directly on top of Node.js built-in net module, so nock doesn't have a clue about the requests done by undici.

@Nytelife26
Copy link
Contributor

It works with all other request libraries, as far as I know?

That is exactly why. undici isn't a request library like others. It is intended to be an alternative to http, not a wrapper for it, and nock uses http, as @piranna correctly said earlier.

@ronag
Copy link
Member

ronag commented May 7, 2021

@mcollina can we close this?

@mcollina mcollina closed this as completed May 7, 2021
@mcollina
Copy link
Member Author

mcollina commented May 7, 2021

Yes it's done

@mrded
Copy link

mrded commented Jan 2, 2024

Yes it's done

@mcollina, can you please share here a link, or an example of how to mock undici requests?

I'm sure many people will end up on this issue, and also wonder how to do that. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: in-progress This issue/pr is currently being worked on
Projects
None yet
Development

No branches or pull requests

9 participants