-
Notifications
You must be signed in to change notification settings - Fork 578
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
MockAgent don't intercept Pool requests #996
Comments
It doesn't and that's the point. Just pass in the agent instead of your pool instance. |
Hiya 👋 Currently working on migrating some code from I have a certificate to use, therefore I must (I think?) create a How would I go about mocking a request which is made using a (I've only tried to use Undici for about an hour now, so I assume I'm just missing something 😅) |
I would recommend you to structure/test your code like the following:
On a side note we probably need a way to specify those client credentials in some global way, the Client/Pool instances are really too low level for most folks. |
This needs better docs. |
* Add constructor argument validation to `FederatedError` to ensure `errors` is an Array * Add input validation tests for FederatedError constructor argument * Fix `request` and `sendRequest` to wrap caught errors in array when creating new `FederatedError` * Add new test file for `request` to ensure array is used * Modify existing `sendRequest` test to verify array is used * Add support for passing in undici agent as part of the federated service configuration. Required for injection during tests. See nodejs/undici#996
* Add constructor argument validation to `FederatedError` to ensure `errors` is an Array * Add input validation tests for FederatedError constructor argument * Fix `request` and `sendRequest` to wrap caught errors in array when creating new `FederatedError` * Add new test file for `request` to ensure array is used * Modify existing `sendRequest` test to verify array is used * Add support for passing in undici agent as part of the federated service configuration. Required for injection during tests. See nodejs/undici#996
I'm facing the same issue. In test I set the global dispatcher as mocked
In the code I have a dynamic range of Clients being created as needed.
However, later, when I call...
...the client does not use the global dispatcher, which has the mocked agent set, and the HTTP call goes out. Of course I could use some factory pattern for clients, and pass different implementation of ClientFactory, based whether I'm running tests or real instance, but I thought the idea of mocking was to avoid all that. |
The idea is to avoid monkeypatching and minimize global state. |
It'd be really awesome if someone has time to write a doc under best practices section on suggested patterns for testing with pools/clients. For what it's worth for future searchers, this is all you need to get a mock pool: import { MockAgent } from "undici";
const mockAgent = new MockAgent();
const mockPool = mockAgent.get("https://www.github.com");
// This mock pool needs to be called instead of the normal pool
mockPool.request({
path: "/",
method: "GET"
}); You'll need to setup your own pattern for passing this pool to be used during tests or getting the mock pool pre-setup if you automate setting it up on test runs. I think a few docs would have saved me a lot of time thinking the mocks weren't working right, because |
Check this example based on these docs https://undici.nodejs.org/#/docs/api/MockPool?id=example-basic-mockpool-instantiation
When
The request never gets intercepted, not sure if I am missing something here
The text was updated successfully, but these errors were encountered: