nodejs/node · error · InvalidArgumentError

UND_ERR_INVALID_ARG

UND_ERR_INVALID_ARG

Error message

Argument opts.agent must implement Agent

What it means

Thrown by the MockPool constructor when opts is missing, opts.agent is missing, or opts.agent.dispatch is not a function. MockPool extends Pool and requires the parent MockAgent to coordinate unmatched dispatches and share state; the agent is duck-typed on the presence of a dispatch method. Identical guard to MockClient's constructor.

Source

Thrown at deps/undici/src/lib/mock/mock-pool.js:26

  kMockAgent,
  kClose,
  kOriginalClose,
  kOrigin,
  kOriginalDispatch,
  kConnected,
  kIgnoreTrailingSlash
} = require('./mock-symbols')
const { MockInterceptor } = require('./mock-interceptor')
const Symbols = require('../core/symbols')
const { InvalidArgumentError } = require('../core/errors')

/**
 * MockPool provides an API that extends the Pool to influence the mockDispatches.
 */
class MockPool extends Pool {
  constructor (origin, opts) {
    if (!opts || !opts.agent || typeof opts.agent.dispatch !== 'function') {
      throw new InvalidArgumentError('Argument opts.agent must implement Agent')
    }

    super(origin, opts)

    this[kMockAgent] = opts.agent
    this[kOrigin] = origin
    this[kIgnoreTrailingSlash] = opts.ignoreTrailingSlash ?? false
    this[kDispatches] = []
    this[kConnected] = 1
    this[kOriginalDispatch] = this.dispatch
    this[kOriginalClose] = this.close.bind(this)

    this.dispatch = buildMockDispatch.call(this)
    this.close = this[kClose]
  }

  get [Symbols.kConnected] () {
    return this[kConnected]

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Obtain a pool through the MockAgent: const pool = mockAgent.get('http://localhost:3000').
  2. If constructing manually, pass the MockAgent: new MockPool(origin, { agent: mockAgent }).
  3. Ensure the agent is a MockAgent instance exposing dispatch.

Example fix

// before
const pool = new MockPool('http://localhost:3000')

// after
const mockAgent = new MockAgent()
const pool = mockAgent.get('http://localhost:3000')
// or
const pool = new MockPool('http://localhost:3000', { agent: mockAgent })
Defensive patterns

Strategy: validation

Validate before calling

function getMockPool(mockAgent, origin) {
  if (!mockAgent || typeof mockAgent.dispatch !== 'function') {
    throw new TypeError('Provide a MockAgent instance');
  }
  return mockAgent.get(origin);
}

Type guard

function isAgentLike(v) {
  return v != null && typeof v.dispatch === 'function';
}

Prevention

When it happens

Trigger: Constructing `new MockPool(origin)` with no opts, opts without agent, or a custom object lacking dispatch. Most commonly hit when instantiating MockPool directly instead of via mockAgent.get(origin) (which returns MockClient or MockPool based on the agent's pooling config).

Common situations: Manually new-ing MockPool in a test instead of using MockAgent.get; passing a real Pool/Agent where a MockAgent was expected; refactoring that drops the agent option.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/be9e34f7a72ae40d. Report an issue: GitHub.