Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks done
StefanLiebscher opened this issue Jul 14, 2024 · 2 comments · May be fixed by #6140
Open
6 tasks done

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

StefanLiebscher opened this issue Jul 14, 2024 · 2 comments · May be fixed by #6140
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@StefanLiebscher
Copy link

StefanLiebscher commented Jul 14, 2024

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

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jul 15, 2024
@sheremet-va
Copy link
Member

Thank you for the bug report. Would you be willing to open a PR with a fix and a test?

@StefanLiebscher
Copy link
Author

StefanLiebscher commented Jul 15, 2024

I'll try... #6140

StefanLiebscher added a commit to StefanLiebscher/vitest that referenced this issue Jul 15, 2024
@StefanLiebscher StefanLiebscher linked a pull request Jul 15, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants