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
- Wrap the tester(s) in an array: `expect.addCustomEqualityTesters([myTester])`.
- If you have multiple testers, pass them all in one array call.
- 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
- Always wrap testers in an array literal at the call site.
- Validate dynamic tester sources before registering.
- Follow the current API signature, not older single-argument examples.
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
- You must provide an array or set to ${matcherHint('.toBeOneO
- Expected is not a string
- You must provide an object to ${this.toString()}, not '${typ
- You must provide an array to ${this.toString()}, not '${type
- any() expects to be passed a constructor function. Please pa
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/3dde1b707e806c1d.
Report an issue: GitHub.