From 85a5f74188d36123ec216f3052c792994719cbf5 Mon Sep 17 00:00:00 2001 From: LeSingh1 Date: Fri, 29 May 2026 17:18:38 -0700 Subject: [PATCH 1/2] fix(expect): toMatchObject failure headline describes a subset match expect(x).toMatchObject(y) is a subset check, not equality, so failing with "Values are not equal." misleads the reader (#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 #6999 --- expect/_build_message.ts | 14 +++++++++++--- expect/_matchers.ts | 7 ++++++- expect/_to_match_object_test.ts | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/expect/_build_message.ts b/expect/_build_message.ts index 4678a46eb106..a208c1a393d2 100644 --- a/expect/_build_message.ts +++ b/expect/_build_message.ts @@ -9,7 +9,14 @@ import type { EqualOptions } from "./_types.ts"; type EqualErrorMessageOptions = Pick< EqualOptions, "formatter" | "msg" ->; +> & { + /** + * Overrides the default "Values are not equal." headline. Set by matchers + * (such as toMatchObject) that don't actually test for equality and would + * otherwise mislead the reader. + */ + summary?: string; +}; function isString(value: unknown): value is string { return typeof value === "string"; @@ -20,12 +27,13 @@ export function buildEqualErrorMessage( expected: T, options: EqualErrorMessageOptions = {}, ): string { - const { formatter = format, msg } = options; + const { formatter = format, msg, summary = "Values are not equal." } = + options; const msgPrefix = msg ? `${msg}: ` : ""; const actualString = formatter(actual); const expectedString = formatter(expected); - let message = `${msgPrefix}Values are not equal.`; + let message = `${msgPrefix}${summary}`; const stringDiff = isString(actual) && isString(expected); const diffResult = stringDiff diff --git a/expect/_matchers.ts b/expect/_matchers.ts index 3c74bf0b7047..f53244ee0065 100644 --- a/expect/_matchers.ts +++ b/expect/_matchers.ts @@ -603,7 +603,12 @@ export function toMatchObject( ); } else { const subset = getObjectSubset(received, expected, context.customTesters); - const defaultMessage = buildEqualErrorMessage(subset, expected); + // toMatchObject is a subset check, not equality. Override the default + // "Values are not equal." headline so the failure message describes + // what actually went wrong (see #6999). + const defaultMessage = buildEqualErrorMessage(subset, expected, { + summary: "Object does not match the expected pattern.", + }); throw new AssertionError( context.customMessage ? `${context.customMessage}: ${defaultMessage}` diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index 8b58b360a21f..0cbc55ddceb9 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -299,3 +299,19 @@ Deno.test("expect().toMatchObject() displays a diff", async (t) => { assertMatch(e.message, /999/); }); }); + +Deno.test( + "expect().toMatchObject() failure headline describes a subset match, not equality (#6999)", + () => { + const e = assertThrows( + () => + expect({ position: { x: 0, y: "string" } }) + .toMatchObject({ position: { x: 0, y: 0 } }), + AssertionError, + ); + // The headline must not claim equality, because toMatchObject is a + // subset check. The diff body is still produced by the same helper. + assertMatch(e.message, /Object does not match the expected pattern\./); + assertNotMatch(e.message, /Values are not equal\./); + }, +); From b3909d8f3aa31a21b9cbddc8e36ec947118bb3c2 Mon Sep 17 00:00:00 2001 From: LeSingh1 Date: Fri, 29 May 2026 20:35:48 -0700 Subject: [PATCH 2/2] fix: deno fmt --- expect/_build_message.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/expect/_build_message.ts b/expect/_build_message.ts index a208c1a393d2..059b30de6a2b 100644 --- a/expect/_build_message.ts +++ b/expect/_build_message.ts @@ -6,17 +6,19 @@ import { diffStr } from "@std/internal/diff-str"; import { format } from "@std/internal/format"; import type { EqualOptions } from "./_types.ts"; -type EqualErrorMessageOptions = Pick< - EqualOptions, - "formatter" | "msg" -> & { - /** - * Overrides the default "Values are not equal." headline. Set by matchers - * (such as toMatchObject) that don't actually test for equality and would - * otherwise mislead the reader. - */ - summary?: string; -}; +type EqualErrorMessageOptions = + & Pick< + EqualOptions, + "formatter" | "msg" + > + & { + /** + * Overrides the default "Values are not equal." headline. Set by matchers + * (such as toMatchObject) that don't actually test for equality and would + * otherwise mislead the reader. + */ + summary?: string; + }; function isString(value: unknown): value is string { return typeof value === "string";