Skip to content

fix: pick up prototype-inherited properties in toMatchObject diff (fix #6939) - #10922

Open
vipulbhasin23 wants to merge 1 commit into
vitest-dev:mainfrom
vipulbhasin23:fix/tomatchobject-prototype-properties
Open

fix: pick up prototype-inherited properties in toMatchObject diff (fix #6939)#10922
vipulbhasin23 wants to merge 1 commit into
vitest-dev:mainfrom
vipulbhasin23:fix/tomatchobject-prototype-properties

Conversation

@vipulbhasin23

Copy link
Copy Markdown

Description

toMatchObject produces 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, where tagName, id, etc. are accessor properties on Element.prototype, not own properties on the instance.

Root cause: getObjectSubset (in packages/expect/src/jest-utils.ts) built the "actual" side of the diff by iterating getObjectKeys(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 with key in object, which walks the prototype chain. subset is always the object literal the caller wrote, so its keys are reliable regardless of what actual is. The existing "N properties omitted from actual" counting logic is unchanged — it still iterates object'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:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Ran the full @vitest/test-unit suite (640 files, 7600 tests, all passing, no type errors) and pnpm test:ci. The only failure in test:ci was a pre-existing, unrelated snapshot mismatch in test-coverage (5.0.0-rc.1 vs 5.0.0-beta.7 version string), unrelated to this change.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

N/A — this is a bugfix to existing behavior, no new functionality introduced.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

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.

@netlify

netlify Bot commented Aug 11, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f7b8d79
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a7b344e76b85c0008930e45
😎 Deploy Preview https://deploy-preview-10922--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@vipulbhasin23

Copy link
Copy Markdown
Author

Checked the failing CI jobs — all 6 e2e matrix failures (ubuntu/macos/windows, node 22/24/26, vite@7) come from the same 2 tests in test/e2e/test/artifacts.test.ts (reporters > verbose non-tty and reporters > default), and they're unrelated to this change:

-  RUN  v<version> <root>
+  RUN  v<version>-rc.1 <root>

The CI environment is running a -rc.1 prerelease build, and the snapshot's version-normalization logic isn't stripping the -rc.1 suffix before comparison. Nothing here touches toMatchObject, getObjectSubset, or packages/expect — this would fail on main too. Locally, all @vitest/expect-related tests pass (@vitest/test-unit: 640 files, 7600 tests, 0 failures), and the Diff/Lint/unit-test-matrix CI jobs (10/11) all passed cleanly on this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad diff output on DOM objects

1 participant