vitest-dev / vitest-dev/vitest
`toEqual` reports `undefined` when other keys do not match
Nobody has claimed this yet.
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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