Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
MockClient extends undici’s Client class and gives you the same intercept API as MockPool but constrained to a single persistent connection. You rarely instantiate MockClient directly — the standard approach is to call mockAgent.get(origin) on a MockAgent configured with connections: 1, which returns a MockClient automatically. Direct instantiation is available for advanced cases where you want to own the lifecycle of a single-origin mock entirely.
Constructor
The origin to mock. Must include the protocol, hostname, and optionally the port — for example
'http://localhost:3000'. No path component.Extends
ClientOptions.Getting a MockClient from MockAgent
The recommended way to obtain aMockClient is through MockAgent:
Obtaining a MockClient via MockAgent
mockClient.intercept(options)
Registers an interceptor on this client. The options and return value are identical to MockPool.intercept().
Chain
.reply(), .replyWithError(), .defaultReplyHeaders(), .defaultReplyTrailers(), .replyContentLength() to define the response. Then chain .persist(), .times(n), or .delay(ms) on the returned MockScope to control repeat behaviour.Intercepting a GET request on MockClient
Additional instance methods
mockClient.cleanMocks()
Removes all registered interceptors from the client.
Clearing all interceptors
mockClient.close()
Closes the client and de-registers it from its parent MockAgent.
Closing a MockClient
mockClient.request(options)
Same as Dispatcher.request(). You can dispatch requests directly through the mock client without going through the global dispatcher.
Direct request through MockClient
When to use MockClient vs MockPool vs MockAgent
In most tests you do not need to choose — use
MockAgent and let it create the right type for you.| Scenario | Recommended class |
|---|---|
| General-purpose mocking across multiple origins | MockAgent + mockAgent.get() (returns MockPool by default) |
| Simulating a single-connection origin (e.g. HTTP/1.1 keep-alive server) | MockAgent({ connections: 1 }) → MockClient |
| Owning mock lifecycle for one origin independently | MockClient directly (advanced) |
| Multiple concurrent connections to one origin | MockPool (via MockAgent) |
MockClient is backed by a single undici Client, meaning requests are serialised through one connection. MockPool allows multiple concurrent connections to the same origin. Choose MockClient when your production code targets a Client directly or when you need to test single-connection semantics.
Complete example
MockClient in a node:test test