remix-run/remix · error · TypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

The "error" argument must be of type function or an instance of Error, RegExp, or Object. Received ${formatReceived(expectedError)}

What it means

assert.throws()/rejects() validates its `error` expectation argument before running. It must be a function (error class), an Error instance, a RegExp (matched against message), or a plain object (partial property match); anything else throws ERR_INVALID_ARG_TYPE.

Source

Thrown at packages/assert/src/lib/assert.ts:173

  if (typeof expectedErrorOrMessage === 'string') {
    return { expectedError: undefined, message: expectedErrorOrMessage }
  }

  return { expectedError: expectedErrorOrMessage, message }
}

function validateExpectedError(expectedError: unknown): void {
  if (
    expectedError == null ||
    typeof expectedError === 'function' ||
    expectedError instanceof Error ||
    expectedError instanceof RegExp ||
    typeof expectedError === 'object'
  ) {
    return
  }

  throw createInvalidArgumentTypeError(
    'error',
    'of type function or an instance of Error, RegExp, or Object',
    expectedError,
  )
}

function validateExpectedDoesNotError(expectedError: unknown): void {
  if (
    expectedError == null ||
    typeof expectedError === 'function' ||
    expectedError instanceof RegExp
  ) {
    return
  }

  throw createInvalidArgumentTypeError(
    'expected',
    'of type function or an instance of RegExp',

View on GitHub (pinned to 9696913134)

Solutions

  1. Wrap string expectations in a RegExp: /expected message/
  2. Use an object for property matching: { code: 'ERR_X' } or { name: 'TypeError' }
  3. Use an error class function for constructor checks

Example fix

// before
assert.throws(() => parse('x'), 'invalid input')
// after
assert.throws(() => parse('x'), /invalid input/)
Defensive patterns

Strategy: type-guard

Validate before calling

const isValidExpectation = (e) =>
  typeof e === 'function' || e instanceof Error || e instanceof RegExp ||
  (typeof e === 'object' && e !== null && Object.keys(e).length > 0)

Type guard

function isErrorExpectation(value: unknown): value is Function | Error | RegExp | object {
  return (
    typeof value === 'function' ||
    value instanceof Error ||
    value instanceof RegExp ||
    (typeof value === 'object' && value !== null)
  )
}

Prevention

When it happens

Trigger: Passing a string, number, or other primitive as the expected-error argument, e.g. assert.throws(fn, 'expected message') (strings are not accepted, unlike some other frameworks).

Common situations: Porting tests from Jest/Chai where a string expectation is allowed, or passing an error code string instead of an object like { code: 'ENOENT' }.

Related errors


AI-assisted analysis of remix-run/remix@9696913134 (2026-08-27). Data as JSON: /api/errors/756eb741d1cb179c. Report an issue: GitHub.