fix: pick up prototype-inherited properties in toMatchObject diff (fix #6939) - #10922
fix: pick up prototype-inherited properties in toMatchObject diff (fix #6939)#10922vipulbhasin23 wants to merge 1 commit into
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Checked the failing CI jobs — all 6 The CI environment is running a |
Description
toMatchObjectproduces an unreadable diff when the actual value has properties that live on its prototype chain instead of as own enumerable properties - for example, DOM elements, wheretagName,id, etc. are accessor properties onElement.prototype, not own properties on the instance.Root cause:
getObjectSubset(inpackages/expect/src/jest-utils.ts) built the "actual" side of the diff by iteratinggetObjectKeys(object), which only returns own enumerable keys. For a DOM element this returns an empty array, so the subset-building loop never ran and the function fell through to returning the raw object — which the pretty-print layer then serialized as<div><img /></div>instead of a plain-object diff.Fix: iterate
getObjectKeys(subset)instead, and check presence withkey in object, which walks the prototype chain.subsetis always the object literal the caller wrote, so its keys are reliable regardless of whatactualis. The existing "N properties omitted from actual" counting logic is unchanged — it still iteratesobject's own keys separately.Known related gap: the one-line summary message (
"expected {...} to match object {...}") stringifies the actual value via a separate code path (utils.getMessage) not touched by this fix, so it may still omit inherited properties there even though the diff body is now correct. Flagging rather than silently leaving it out of scope — happy to follow up separately if useful.Resolves #6939
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Ran the full
@vitest/test-unitsuite (640 files, 7600 tests, all passing, no type errors) andpnpm test:ci. The only failure intest:ciwas a pre-existing, unrelated snapshot mismatch intest-coverage(5.0.0-rc.1vs5.0.0-beta.7version string), unrelated to this change.Documentation
pnpm run docscommand.N/A — this is a bugfix to existing behavior, no new functionality introduced.
Changesets
feat:,fix:,perf:,docs:, orchore:.I used Claude as an assistant while investigating and tracing the root cause through the codebase; the diagnosis, fix, and test above are ones I understand and can walk through.