Repository navigation
fix(expect): do not leak subset equality into asymmetric matchers - #11093
pri12ya871 wants to merge 1 commit into
Conversation
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
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
The one red check here looks unrelated to this change, so noting what I found rather than leaving it unexplained.
The same job passes on node-22 in this very run, and I could not find an existing issue for it. |
|
Hello @pri12ya871. Your PR has been labeled To keep your PR open, please follow these steps:
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. |
Description
toMatchObjectcompares withsubsetEquality, 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.ArrayContainingcompares 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: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
subsetEqualityno 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 outsidetoMatchObject.Deliberately scoped so that nothing else moves:
objectContaining, which does its own subset-style check, is unaffectedHow 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 fulltest/unitsuite is also clean against this change: the only failures on my machine are@vitest/web-workerresolution (a package I had not built locally) and onejsdom-optionstimeout that passes in isolation at 3.0s against a 5s limit — neither touches matcher behaviour.eslintis clean on both changed files.