method

test.Expect.addEqualityTesters

testers: Tester[]
): void;

Registers functions that decide whether two values are equal. They're used by toEqual(), toStrictEqual(), toMatchObject(), toHaveBeenCalledWith() and every other matcher and asymmetric matcher that compares deeply.

A tester returns true or false. It can also return undefined to pass the pair on to the next tester, and then to the built-in rules. Testers registered by a test file last until that file ends. Testers registered by a preload script apply to every file.

expect.addEqualityTesters([
  (a, b) => (a instanceof Volume && b instanceof Volume ? a.equals(b) : undefined),
]);
expect(new Volume(1, "L")).toEqual(new Volume(1000, "mL"));

Referenced types

type Tester = (this: TesterContext, a: any, b: any, customTesters: Tester[]) => boolean | undefined

Custom equality tester