vitest-dev/vitest · error · TypeError

expect.customEqualityTesters: Must be set to an array of Tes

Error message

expect.customEqualityTesters: Must be set to an array of Testers. Was given "${getType(newTesters)}"

What it means

`addCustomEqualityTesters` (exposed as `expect.customEqualityTesters`) pushes testers onto a global array. If the caller passes something that is not an array, the call is rejected with a `TypeError` before any tester is registered. The message reports the runtime type via `getType`.

Source

Thrown at packages/expect/src/jest-matcher-utils.ts:145

}

export function printWithType<T>(
  name: string,
  value: T,
  print: (value: T) => string,
): string {
  const type = getType(value)
  const hasType
    = type !== 'null' && type !== 'undefined'
      ? `${name} has type:  ${type}\n`
      : ''
  const hasValue = `${name} has value: ${print(value)}`
  return hasType + hasValue
}

export function addCustomEqualityTesters(newTesters: Array<Tester>): void {
  if (!Array.isArray(newTesters)) {
    throw new TypeError(
      `expect.customEqualityTesters: Must be set to an array of Testers. Was given "${getType(
        newTesters,
      )}"`,
    )
  }

  (globalThis as any)[JEST_MATCHERS_OBJECT].customEqualityTesters.push(
    ...newTesters,
  )
}

export function getCustomEqualityTesters(): Array<Tester> {
  return (globalThis as any)[JEST_MATCHERS_OBJECT].customEqualityTesters
}

View on GitHub (pinned to 1fa9837ec2)

Solutions

  1. Wrap the tester(s) in an array: `expect.addCustomEqualityTesters([myTester])`.
  2. If you have multiple testers, pass them all in one array call.
  3. Check the value is an array before calling (e.g. when testers come from a dynamic source).

Example fix

// before
expect.addCustomEqualityTesters(myTester)

// after
expect.addCustomEqualityTesters([myTester])
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(testers)) {
  throw new TypeError('expect.addCustomEqualityTesters requires an array')
}
expect.addCustomEqualityTesters(testers)

Type guard

const isTesterArray = (v: unknown): v is Array<(a: unknown, b: unknown) => void | undefined> =>
  Array.isArray(v)

Prevention

When it happens

Trigger: Calling `expect.addCustomEqualityTesters(fn)` or `expect.customEqualityTesters = fn` with a single function instead of an array; passing `undefined`, an object, or a string.

Common situations: Following an older or incorrect example that passes a single tester; misreading the API signature; migrating from Jest where the array wrapping was implicit.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/3dde1b707e806c1d. Report an issue: GitHub.