Skip to content

fix(expect): do not leak subset equality into asymmetric matchers - #11093

Closed
pri12ya871 wants to merge 1 commit into
vitest-dev:mainfrom
pri12ya871:fix/array-containing-inside-match-object
Closed

pri12ya871 wants to merge 1 commit into
vitest-dev:mainfrom
pri12ya871:fix/array-containing-inside-match-object

Conversation

@pri12ya871

Copy link
Copy Markdown

Description

toMatchObject compares with subsetEquality, which passes itself down as a custom tester so that nested plain objects keep subset semantics. That tester was also handed to any asymmetric matcher found in the expected object — and a matcher runs its own comparison with whatever testers it is given.

ArrayContaining compares its sample as the left-hand side (equals(item, another, customTesters)), so with a subset tester in play an actual element only had to be a subset of the sample. Every field the sample declared beyond those present on the actual element was ignored:

expect({ records: [{ id: 1 }] }).toMatchObject({
  records: expect.arrayContaining([{ id: 1, required: 'must not match without this' }]),
})
// passes — but the actual record has no `required` field

Jest fails this correctly, because its matchers use the testers they were constructed with rather than ones propagated into them.

Reported in #11071 by @KalininAndreyVictorovich.

The fix

subsetEquality no longer adds its own recursive tester when the expected value at a key is an asymmetric matcher. The matcher then compares with the plain testers, as it does outside toMatchObject.

Deliberately scoped so that nothing else moves:

  • nested plain objects keep subset semantics
  • an actual element may still carry extra fields the sample does not mention
  • objectContaining, which does its own subset-style check, is unaffected

How I verified it

Four tests in test/unit/test/expect.test.ts. The first fails without this change (expected [Function] to throw an error) and passes with it; the other three pass both with and without, which is the point — they pin the behaviour that must not change.

vitest run --root test/unit test/expect.test.ts test/diff.test.ts → 237 passed, 3 expected fail, 0 failures. The full test/unit suite is also clean against this change: the only failures on my machine are @vitest/web-worker resolution (a package I had not built locally) and one jsdom-options timeout that passes in isolation at 3.0s against a 5s limit — neither touches matcher behaviour.

eslint is clean on both changed files.

toMatchObject compares with subsetEquality, which passes itself down as
a custom tester so nested plain objects keep subset semantics. That
tester was also handed to any asymmetric matcher found in the expected
object, and a matcher runs its own comparison with whatever testers it
is given.

arrayContaining compares its sample as the left-hand side —
equals(item, another) — so with a subset tester in play an actual
element only had to be a subset of the sample. Every field the sample
declared beyond those on the actual element was ignored, and the
assertion passed when it should have failed:

  expect({ records: [{ id: 1 }] }).toMatchObject({
    records: expect.arrayContaining([{ id: 1, required: 'x' }]),
  })

Jest fails this correctly, because its matchers use the testers they
were constructed with rather than the ones propagated into them.

Subset semantics still apply to nested plain objects, and an actual
element may still carry extra fields — only the matcher's own
comparison is now insulated.

Closes vitest-dev#11071
@netlify

netlify Bot commented Aug 29, 2026

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 865c5e1
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a9285a8dae58f000885df4d
😎 Deploy Preview https://deploy-preview-11093--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.

@pri12ya871

Copy link
Copy Markdown
Author

The one red check here looks unrelated to this change, so noting what I found rather than leaving it unexplained.

Test: e2e, node-26 fails on test/detect-async-leaks.test.ts > fs handle, expecting a leak report and getting an empty string:

AssertionError: expected '' to contain 'FSREQCALLBACK leaking in packages/example/test/example.test.ts…'

The same job passes on node-22 in this very run, and Test: coverage, node-26 passes too — so it is specific to async-leak detection under Node 26, not to Node 26 generally. This PR only changes how subsetEquality composes custom testers, which cannot vary by Node version, and every expect-related job is green.

I could not find an existing issue for it. main has not had a CI run since 22 August, so I could not compare against a baseline directly — if node-26 was added to the matrix since then, this may be its first appearance rather than a regression from any particular PR. Happy to open a separate issue if that is useful.

@sheremet-va sheremet-va added the maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control label Aug 29, 2026
@github-actions

Copy link
Copy Markdown

Hello @pri12ya871. Your PR has been labeled maybe automated because it appears to have been fully generated by AI with no human involvement.

To keep your PR open, please follow these steps:

  • Confirm that you are a real human. If you are an automated agent, disclose that
  • Make sure you've read, reviewed and stand behind its content
  • Make sure you've read the full issue along with all of its comments, as well as any linked issues and their comments
  • Make sure it follows our contribution guidelines and uses the correct GitHub template
  • Disclose any AI tools you used (e.g. Claude, Copilot, Codex)

Please, do not generate or format the response with AI. If you do not speak English, reply in your native language or use translation software like Google Translate or Deepl. If the response is generated, the PR will be closed automatically.

These measures help us reduce maintenance burden and keep the team's work efficient. See our AI contributions policy for more context.

@github-actions github-actions Bot closed this Aug 31, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants