vitest-dev / vitest-dev/vitest

`toEqual` reports `undefined` when other keys do not match

Open
#7,083 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

p3-minor-bug
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in test/toEqual.test.ts and run the provided toEqual case. Trace the object-equality reporting used by that test; done means the mismatch still reports the differing qux value without including the extra undefined key, while preserving the existing behavior when no other key differs.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.