jestjs/jest · error · TypeError

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

Error message

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

What it means

expect.addEqualityTesters (which sets customEqualityTesters) requires an array of Tester functions used to extend the equals algorithm. addCustomEqualityTesters throws a TypeError if the argument is not an array, because it spreads the testers into the global registry and a non-array would either spread incorrectly or push the wrong shape. The message reports the actual type via getType.

Solutions

  1. Wrap testers in an array: `expect.addEqualityTesters([myTester])`.
  2. If you have multiple testers, pass them all in one array: `expect.addEqualityTesters([testerA, testerB])`.
  3. Confirm each entry is a Tester function (signature (a, b, customTesters) => 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

function addTestersSafe(testers) {
  if (!Array.isArray(testers)) {
    throw new TypeError('expect.addEqualityTesters requires an array of Tester functions');
  }
  expect.addEqualityTesters(testers);
}

Type guard

function isTesterArray(v: unknown): v is Array<(a: unknown, b: unknown) => boolean | undefined> {
  return Array.isArray(v) && v.every(t => typeof t === 'function');
}

Prevention

When it happens

Trigger: Calling `expect.addEqualityTesters(value)` where value is not an array — e.g. a single tester function passed without wrapping in [], or an object/map of testers.

Common situations: Passing a single function instead of an array of functions; passing an object keyed by name expecting it to be merged; refactoring from a previous API that accepted a single tester.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/269047bcf82a2826. Report an issue: GitHub.

Appendix: 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 8e6d128e4a)