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

toEqual reports undefined when other keys do not match #7083

Open
6 tasks done
AnthonyLzq opened this issue Dec 14, 2024 · 0 comments · May be fixed by #7134
Open
6 tasks done

toEqual reports undefined when other keys do not match #7083

AnthonyLzq opened this issue Dec 14, 2024 · 0 comments · May be fixed by #7134
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@AnthonyLzq
Copy link

Describe the bug

Consider the following case:

const obj1 = {
  foo: 'bar',
  qux: 'qux'
}

const obj2 = {
  foo: 'bar',
  extra: undefined,
  qux: 'qux2'
}

If you are using toEqual, since it should not take care about undefined, you should expect the following comparison to fail:

expect(obj1).toEqual(obj2)

Because of the qux key, but the error also takes in consideration the undefined value, according to the following error:

FAIL  test/toEqual.test.ts > Object equality > obj2 should equal obj1
AssertionError: expected { foo: 'bar', extra: undefined, …(1) } to deeply equal { foo: 'bar', qux: 'qux' }

- Expected
+ Received

  Object {
+   "extra": undefined,
    "foo": "bar",
-   "qux": "qux",
+   "qux": "qux2",
  }

Which seems to be inconsistent, because if you remove the qux key, toEqual does not complain about the undefined.

Reproduction

Just use the following test:

import { describe, expect, test } from 'vitest'

const obj1 = {
  foo: 'bar',
  qux: 'qux'
}

const obj2 = {
  foo: 'bar',
  extra: undefined,
  qux: 'qux2'
}

describe('Object equality', () => {
  test('obj1 should equal obj2', () => {
    expect(obj1).toEqual(obj2)
  })
})

System Info

System:
  OS: macOS 15.2
  CPU: (8) arm64 Apple M1
  Memory: 184.66 MB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.15.0 - ~/.nvm/versions/node/v20.15.0/bin/node
  npm: 10.7.0 - ~/.nvm/versions/node/v20.15.0/bin/npm
  pnpm: 9.4.0 - ~/.nvm/versions/node/v20.15.0/bin/pnpm
Browsers:
  Brave Browser: 131.1.73.101
  Chrome: 102.0.5005.61
  Safari: 18.2
npmPackages:
  vitest: ^2.1.8 => 2.1.8

Used Package Manager

npm

Validations

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Dec 15, 2024
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
2 participants