nodejs/node · error · InvalidArgumentError

UND_ERR_INVALID_ARG

UND_ERR_INVALID_ARG

Error message

waitInMs must be a valid integer > 0

What it means

Thrown by MockScope.delay when waitInMs is not a number, not an integer, or not greater than zero. delay sets a response latency on a mock dispatch; fractional, zero, or negative durations are rejected so the delay timer is always well-defined.

Source

Thrown at deps/undici/src/lib/mock/mock-interceptor.js:34

  types: {
    isPromise
  }
} = require('node:util')

/**
 * Defines the scope API for an interceptor reply
 */
class MockScope {
  constructor (mockDispatch) {
    this[kMockDispatch] = mockDispatch
  }

  /**
   * Delay a reply by a set amount in ms.
   */
  delay (waitInMs) {
    if (typeof waitInMs !== 'number' || !Number.isInteger(waitInMs) || waitInMs <= 0) {
      throw new InvalidArgumentError('waitInMs must be a valid integer > 0')
    }

    this[kMockDispatch].delay = waitInMs
    return this
  }

  /**
   * For a defined reply, never mark as consumed.
   */
  persist () {
    this[kMockDispatch].persist = true
    return this
  }

  /**
   * Allow one to define a reply for a set amount of matching requests.
   */
  times (repeatTimes) {

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass a positive integer of milliseconds: delay(500).
  2. Omit delay() entirely when no delay is desired.
  3. Coerce and clamp config values: delay(Math.max(1, Math.floor(config.delay))).

Example fix

// before
intercept.reply(200).delay(config.timeout)
intercept.reply(200).delay(0)

// after
intercept.reply(200).delay(Math.max(1, Number(config.timeout)))
intercept.reply(200) // no delay needed
Defensive patterns

Strategy: validation

Validate before calling

function safeDelay(scope, ms) {
  if (typeof ms !== 'number' || !Number.isInteger(ms) || ms <= 0) {
    throw new TypeError('delay expects a positive integer ms');
  }
  return scope.delay(ms);
}

Type guard

function isPositiveIntegerMs(v) {
  return typeof v === 'number' && Number.isInteger(v) && v > 0;
}

Prevention

When it happens

Trigger: Calling interceptor.reply(200).delay(0), delay(-100), delay(1.5), delay('500'), or delay(undefined). Also delay() with no argument (undefined).

Common situations: Passing a delay from a config file as a string, using 0 intending 'no delay' (use omit instead), or computing a delay that can go non-positive.

Related errors


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