vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.17k stars 1.19k forks source link

Set comparison within expect.objectContaining fails #6820

Open danny-does-stuff opened 1 month ago

danny-does-stuff commented 1 month ago

Describe the bug

When comparing Sets within expect.objectContaining, any Set will make the test pass. In other words, the line expect({ set: new Set() }).toEqual(expect.objectContaining({ set: new Set([1]) })); does not cause the test to fail.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-dpdxnv?file=test%2Fbasic.test.ts

System Info

System:
    OS: macOS 15.0.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 34.01 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
  Browsers:
    Brave Browser: 128.1.69.168
    Chrome: 109.0.5414.87
    Edge: 130.0.2849.56
    Safari: 18.0.1
    Safari Technology Preview: 18.0

Used Package Manager

yarn

Validations

hi-ogawa commented 1 month ago

I checked Jest has the same behavior https://stackblitz.com/edit/github-vktwdn?file=basic.test.js, but I don't know for what reason this would make sense. It's probably a bug and should be fixed.

hi-ogawa commented 1 month ago

It looks like asymmetric matcher's equality doesn't consider iterableEquality, so arrayContaining doesn't work either https://stackblitz.com/edit/vitest-dev-vitest-4n18bx?file=test%2Fbasic.test.ts

import { expect, test } from 'vitest';

test('repro 1', () => {
  expect([new Set()]).toEqual(expect.arrayContaining([new Set([1])]));
});