Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions expect/_build_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,38 @@ export function buildNotEqualErrorMessage<T>(
const msgPrefix = msg ? `${msg}: ` : "";
return `${msgPrefix}Expected actual: ${actualString} not to be: ${expectedString}.`;
}

export function buildToMatchObjectErrorMessage<T>(
actual: T,
expected: T,
options: EqualErrorMessageOptions = {},
): string {
const { formatter = format, msg } = options;
const msgPrefix = msg ? `${msg}: ` : "";
const actualString = formatter(actual);
const expectedString = formatter(expected);

let message = `${msgPrefix}expect(received).toMatchObject(expected)`;

const stringDiff = isString(actual) && isString(expected);
const diffResult = stringDiff
? diffStr(actual, expected)
: diff(actualString.split("\n"), expectedString.split("\n"));
const diffMsg = buildMessage(diffResult, { stringDiff }).join("\n");
message = `${message}\n${diffMsg}`;

return message;
}

export function buildNotToMatchObjectErrorMessage<T>(
actual: T,
expected: T,
options: EqualErrorMessageOptions = {},
): string {
const { formatter = format, msg } = options;
const actualString = formatter(actual);
const expectedString = formatter(expected);

const msgPrefix = msg ? `${msg}: ` : "";
return `${msgPrefix}expect(received).not.toMatchObject(expected)\n\nExpected: not ${expectedString}\nReceived: ${actualString}`;
}
10 changes: 7 additions & 3 deletions expect/_matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
} from "./_utils.ts";
import {
buildEqualErrorMessage,
buildNotEqualErrorMessage,
buildNotToMatchObjectErrorMessage,
buildToMatchObjectErrorMessage,
} from "./_build_message.ts";
import {
escapeStringForJs,
Expand Down Expand Up @@ -595,15 +596,18 @@ export function toMatchObject(

const triggerError = () => {
if (context.isNot) {
const defaultMessage = buildNotEqualErrorMessage(received, expected);
const defaultMessage = buildNotToMatchObjectErrorMessage(
received,
expected,
);
throw new AssertionError(
context.customMessage
? `${context.customMessage}: ${defaultMessage}`
: defaultMessage,
);
} else {
const subset = getObjectSubset(received, expected, context.customTesters);
const defaultMessage = buildEqualErrorMessage(subset, expected);
const defaultMessage = buildToMatchObjectErrorMessage(subset, expected);
throw new AssertionError(
context.customMessage
? `${context.customMessage}: ${defaultMessage}`
Expand Down
12 changes: 11 additions & 1 deletion expect/_to_match_object_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,24 @@ Deno.test("expect().toMatchObject() throws the correct error messages", () => {
() => expect({ a: 1 }).toMatchObject({ a: 2 }),
AssertionError,
);
// The matcher name must appear in the message, not a generic equality
// phrase such as "Values are not equal." which misleads users into
// thinking strict equality was checked.
assertMatch(e.message, /expect\(received\)\.toMatchObject\(expected\)/);
assertNotMatch(e.message, /Values are not equal/);
assertNotMatch(e.message, /not to be/);
}
{
const e = assertThrows(
() => expect({ a: 1 }).not.toMatchObject({ a: 1 }),
AssertionError,
);
assertMatch(e.message, /not to be/);
assertMatch(
e.message,
/expect\(received\)\.not\.toMatchObject\(expected\)/,
);
assertNotMatch(e.message, /Values are equal/);
assertNotMatch(e.message, /Expected actual: .* not to be:/);
}
});

Expand Down
Loading