Skip to content

fix(expect): toMatchObject failure headline describes a subset match#7168

Open
LeSingh1 wants to merge 2 commits into
denoland:mainfrom
LeSingh1:expect-tomatchobject-message
Open

fix(expect): toMatchObject failure headline describes a subset match#7168
LeSingh1 wants to merge 2 commits into
denoland:mainfrom
LeSingh1:expect-tomatchobject-message

Conversation

@LeSingh1
Copy link
Copy Markdown

Fixes #6999.

expect(x).toMatchObject(y) is a subset check, not equality, so failing with Values are not equal. misleads the reader into thinking the test is asking for strict equality. From the issue's repro:

error: AssertionError: Values are not equal.

    [Diff] Actual / Expected

    {
      position: {
+       x: Any { inverse: false, value: [Function: Number] },
+       y: Any { inverse: false, value: [Function: Number] },
-       x: 0,
-       y: "string",
      },
    }

After:

error: AssertionError: Object does not match the expected pattern.

    [Diff] Actual / Expected

    {
      position: {
        x: 0,
-       y: "string",
+       y: 0,
      },
    }

Implementation: buildEqualErrorMessage now accepts an optional summary override on its options. Default is "Values are not equal." so toEqual and the other equality matchers are unchanged. toMatchObject passes summary: "Object does not match the expected pattern." so the headline matches what it actually checks. The diff body and the customMessage prefix are untouched.

Test added in expect/_to_match_object_test.ts pins the new headline and verifies the old one is no longer present. The full --filter toMatchObject run passes (7/7).

expect(x).toMatchObject(y) is a subset check, not equality, so failing
with "Values are not equal." misleads the reader (denoland#6999). Add an
optional `summary` override to buildEqualErrorMessage and have
toMatchObject pass "Object does not match the expected pattern." The
diff body is unchanged and still produced by the same helper. toEqual
and other equality matchers keep the original headline.

Test pins the new headline and confirms the old one is no longer
present.

Fixes denoland#6999
@codecov
Copy link
Copy Markdown

codecov Bot commented May 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.57%. Comparing base (cdf74a8) to head (b3909d8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7168      +/-   ##
==========================================
- Coverage   94.57%   94.57%   -0.01%     
==========================================
  Files         636      636              
  Lines       52142    52145       +3     
  Branches     9401     9401              
==========================================
+ Hits        49315    49317       +2     
  Misses       2249     2249              
- Partials      578      579       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expect(...).toMatchObject error is "AssertionError: Values are not equal." instead of

1 participant