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.
MockPool extends undici’s Pool class and is the default mock dispatcher returned by mockAgent.get(origin). It supports multiple concurrent connections to a single origin and provides the full intercept API for matching and replying to HTTP requests. All interceptors you register are consumed in the order they were added — each one fires once by default, unless you call .persist() or .times(n) on the returned scope.
Constructor
The origin to mock. Must include the protocol, hostname, and optionally the port — for example
'http://localhost:3000'. No path component.Extends
PoolOptions.Getting a MockPool from MockAgent
Obtaining a MockPool via MockAgent
mockPool.intercept(options)
Registers an interceptor for a specific request pattern. All defined options must match for an interceptor to fire. Returns a MockInterceptor that you chain to define the response.
A
MockInterceptor instance for defining the response.MockInterceptor — defining responses
.reply(statusCode, body, options?)
HTTP status code of the mocked response.
Response body. Objects are JSON-serialised; strings and buffers are sent as-is. A callback receives
{ method, url, body, headers, origin } and must return the body value..reply(callback)
Alternative signature — a single callback that returns the full options object { statusCode, data, headers?, trailers? }.
.replyWithError(error)
Causes the matched request to throw the given Error.
.defaultReplyHeaders(headers)
Merges these headers into every subsequent reply() on this interceptor. Specific reply() headers take precedence.
.defaultReplyTrailers(trailers)
Merges these trailers into every subsequent reply().
.replyContentLength()
Automatically adds a content-length header calculated from the reply body.
MockScope — controlling repeat behaviour
reply() and replyWithError() return a MockScope. Chain these methods to control how many times the interceptor fires:
The interceptor matches indefinitely. Without this, interceptors are single-use.
The interceptor matches exactly
n times.Delays the response by
ms milliseconds before replying.Matcher reference
Everypath, method, body, and header value supports three matcher types:
| Matcher | How it works |
|---|---|
string | Exact string equality |
RegExp | regex.test(value) must be true |
Function | Function must return true |
Examples
Basic mocked request
Simple GET intercept
POST with body, headers, and trailers
POST with full request and response matching
Dynamic reply with a callback
Echo reply using a data callback
Reply with an error
Simulating a connection error
Default reply headers
Setting default headers on an interceptor
Automatic content-length
Auto-calculated content-length
Persistent interceptors
Interceptor that matches every time
Fixed-count interceptors
Interceptor that matches twice
Function path matcher
Custom path matching function
Additional instance methods
mockPool.cleanMocks()
Removes all registered interceptors from the pool.
Clearing all interceptors
mockPool.close()
Closes the pool and de-registers it from its parent MockAgent.
Closing a MockPool
mockPool.request(options)
Dispatches a request directly through this mock pool, bypassing the global dispatcher.
Direct request through MockPool
When to use MockPool vs MockClient vs MockAgent
| Scenario | Recommended choice |
|---|---|
| Multiple concurrent connections to one origin | MockPool (default from mockAgent.get()) |
| Single-connection semantics | MockClient via MockAgent({ connections: 1 }) |
| Multiple origins in one test | MockAgent + mockAgent.get() per origin |
MockPool is the default because most production code uses a Pool or the default Agent. Prefer MockClient only when your code explicitly targets a Client.