jestjs/jest · 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 (the impl behind expect.addEqualityTesters) requires its argument be an array; it then spreads it into the global customEqualityTesters registry. A non-array would either be iterated incorrectly or silently merged, so Jest validates with Array.isArray and throws TypeError with getType() of the offending value.
Source
Thrown at packages/expect/src/jestMatchersObject.ts:134
Object.defineProperty(expect.not, key, {
configurable: true,
enumerable: true,
value: (...sample: [unknown, ...Array<unknown>]) =>
new CustomMatcher(true, ...sample),
writable: true,
});
}
}
Object.assign((globalThis as any)[JEST_MATCHERS_OBJECT].matchers, matchers);
};
export const getCustomEqualityTesters = (): Array<Tester> =>
(globalThis as any)[JEST_MATCHERS_OBJECT].customEqualityTesters;
export const 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,
);
};
View on GitHub (pinned to f49721c78e)
Solutions
- Wrap testers in an array: expect.addEqualityTesters([myTester]).
- Add multiple at once: expect.addEqualityTesters([testerA, testerB]).
- Confirm each entry is a Tester function (signature (a, b, ctx) => boolean | undefined).
Example fix
// before expect.addEqualityTesters(myDateTester); // single function, not an array // after expect.addEqualityTesters([myDateTester]);
Defensive patterns
Strategy: type-guard
Validate before calling
if (!Array.isArray(testers)) {
throw new TypeError('expect.addEqualityTesters needs an array of Tester functions');
}
expect.addEqualityTesters(testers); Type guard
function isTesterArray(x: unknown): x is Array<(a: unknown, b: unknown) => boolean | undefined> {
return Array.isArray(x) && x.every(t => typeof t === 'function');
} Try / catch
// addCustomEqualityTesters throws synchronously — wrap single testers in an array before calling
Prevention
- Always pass an array literal: expect.addEqualityTesters([t]).
- Type the parameter as Array<Tester> so TS rejects single functions.
- Confirm each entry is a Tester-shaped function before adding.
When it happens
Trigger: Calling expect.addEqualityTesters(myTester) with a single function instead of [myTester], passing null/undefined, or passing an object of testers by mistake.
Common situations: Reading the API as 'add a tester' (singular) and passing one function; copy-paste from a non-array source; migrating from a config where testers were keyed by name.
Related errors
- any() expects to be passed a constructor function. Please pa
- @jest/diff-sequences: ${name} typeof ${typeof arg} is not a
- @jest/diff-sequences: ${name} typeof ${type} is not a functi
- You must provide an array to ${this.toString()}, not '${type
- You must provide an object to ${this.toString()}, not '${typ
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/269047bcf82a2826.json.
Report an issue: GitHub.