Skip to content

feat(expect): warn on toEqual comparing non-plain objects with empty Object.keys#9894

Open
ViniciusDev26 wants to merge 5 commits intovitest-dev:mainfrom
ViniciusDev26:feat/warn-toequal-non-plain-empty-objects
Open

feat(expect): warn on toEqual comparing non-plain objects with empty Object.keys#9894
ViniciusDev26 wants to merge 5 commits intovitest-dev:mainfrom
ViniciusDev26:feat/warn-toequal-non-plain-empty-objects

Conversation

@ViniciusDev26
Copy link
Copy Markdown

@ViniciusDev26 ViniciusDev26 commented Mar 17, 2026

Description

Adds isPlainObject and isNonPlainEmptyObject helpers to jest-utils and emits a console.warn in toEqual when the actual value
is a non-plain object with no enumerable keys, preventing silent false-negatives (e.g. Response).

Example of the false-negative this addresses:

// This test passes silently today, but should not:
const r1 = new Response("hoge", { status: 200 })
const r2 = new Response(null, { status: 404 })
expect(r1).toEqual(r2) // ⚠ no properties compared                                                                                      

Resolves #9542

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.

Documentation

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

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:.

…Object.keys

Adds isPlainObject and isNonPlainEmptyObject helpers to jest-utils and
emits a console.warn in toEqual when both sides are non-plain objects
with no enumerable keys, preventing silent false-negatives (e.g. Response).

Closes vitest-dev#9542
@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 17, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit dccfa8e
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69b9cdc1cfdd8000080fc0e4
😎 Deploy Preview https://deploy-preview-9894--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

Copy link
Copy Markdown
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue has a comment on the board:

have a builtin response/request check, check what node does in assert

We want to process Response/Request/Headers inside the equals function. And we need to validate how the native assert in Node works with those already

We don't want to add the empty check guard

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.

Show warning if toEqual compares the object which Object.keys returns empty array for

2 participants