Skip to content

Custom equality testers are not properly relayed to typed arrays #6122

@StefanLiebscher

Description

@StefanLiebscher

Describe the bug

When adding a custom equality tester in vitest.setup, for example

function numberEquality(actual, expected) {
  return typeof expected === 'number' ? actual === expected : undefined;
}
expect.addEqualityTesters([
  numberEquality
]);

it is not relayed to typed-array values in toStrictEqual matches.

A patch to fix the issue would be

diff --git a/node_modules/@vitest/expect/dist/index.js b/node_modules/@vitest/expect/dist/index.js
index 868ffa8..c9c2d0e 100644
--- a/node_modules/@vitest/expect/dist/index.js
+++ b/node_modules/@vitest/expect/dist/index.js
@@ -433,7 +433,7 @@ function iterableEquality(a, b, customTesters = [], aStack = [], bStack = []) {
   if (!isImmutableList(a) && !isImmutableOrderedKeyed(a) && !isImmutableOrderedSet(a) && !isImmutableRecord(a)) {
     const aEntries = Object.entries(a);
     const bEntries = Object.entries(b);
-    if (!equals(aEntries, bEntries)) {
+    if (!equals(aEntries, bEntries, filteredCustomTesters)) {
       return false;
     }
   }

Reproduction

describe('the numberEquality equality tester', () => {
 // works 
 it('attest equality of -0 and +0 within arrays', () => {
    expect([-0, 0, 0]).toStrictEqual([0, -0, 0]);
  });

  // fails without patch
  it('attest equality of -0 and +0 within typed arrays', () => {
    expect(Float64Array.of(-0, 0, 0)).toStrictEqual(Float64Array.of(0, -0, 0));
  });
});

System Info

System:
    OS: Linux 6.6 Gentoo Linux
  Binaries:
    Node: 20.12.1 - /usr/bin/node
    npm: 10.5.0 - /usr/bin/npm
  npmPackages:
    @vitest/coverage-istanbul: 2.0.2 => 2.0.2 
    @vitest/coverage-v8: 2.0.2 => 2.0.2 
    @vitest/expect: 2.0.2 => 2.0.2 
    vite: 5.3.3 => 5.3.3 
    vitest: 2.0.2 => 2.0.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions