Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): always show custom message #6217

Merged
merged 14 commits into from
Dec 17, 2024
2 changes: 1 addition & 1 deletion expect/_assert_equals_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Deno.test({
() => assertEquals(1, 2, { msg: "CUSTOM MESSAGE" }),
AssertionError,
[
"Values are not equal: CUSTOM MESSAGE",
"CUSTOM MESSAGE: Values are not equal.",
...createHeader(),
removed(`- ${yellow("1")}`),
added(`+ ${yellow("2")}`),
Expand Down
54 changes: 54 additions & 0 deletions expect/_assert_is_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { AssertionError } from "@std/assert/assertion-error";
import { stripAnsiCode } from "@std/internal/styles";

/**
* Make an assertion that `error` is an `Error`.
* If not then an error will be thrown.
* An error class and a string that should be included in the
* error message can also be asserted.
*
* @typeParam E The type of the error to assert.
* @param error The error to assert.
* @param ErrorClass The optional error class to assert.
* @param msgMatches The optional string or RegExp to assert in the error message.
* @param msg The optional message to display if the assertion fails.
*/
export function assertIsError<E extends Error = Error>(
error: unknown,
// deno-lint-ignore no-explicit-any
ErrorClass?: abstract new (...args: any[]) => E,
msgMatches?: string | RegExp,
msg?: string,
): asserts error is E {
const msgPrefix = msg ? `${msg}: ` : "";
if (!(error instanceof Error)) {
throw new AssertionError(
`${msgPrefix}Expected "error" to be an Error object.`,
);
}
if (ErrorClass && !(error instanceof ErrorClass)) {
msg =
`${msgPrefix}Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}".`;
throw new AssertionError(msg);
}
let msgCheck;
if (typeof msgMatches === "string") {
msgCheck = stripAnsiCode(error.message).includes(
stripAnsiCode(msgMatches),
);
}
if (msgMatches instanceof RegExp) {
msgCheck = msgMatches.test(stripAnsiCode(error.message));
}

if (msgMatches && !msgCheck) {
msg = `${msgPrefix}Expected error message to include ${
msgMatches instanceof RegExp
? msgMatches.toString()
: JSON.stringify(msgMatches)
}, but got ${JSON.stringify(error?.message)}.`;
throw new AssertionError(msg);
}
}
2 changes: 1 addition & 1 deletion expect/_assert_not_equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export function assertNotEquals<T>(
return;
}

const message = buildNotEqualErrorMessage(actual, expected, options);
const message = buildNotEqualErrorMessage(actual, expected, options ?? {});
throw new AssertionError(message);
}
8 changes: 4 additions & 4 deletions expect/_build_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function buildEqualErrorMessage<T>(
options: EqualErrorMessageOptions,
): string {
const { formatter = format, msg } = options ?? {};
const msgSuffix = msg ? `: ${msg}` : ".";
const msgPrefix = msg ? `${msg}: ` : "";
const actualString = formatter(actual);
const expectedString = formatter(expected);

let message = `Values are not equal${msgSuffix}`;
let message = `${msgPrefix}Values are not equal.`;

const stringDiff = isString(actual) && isString(expected);
const diffResult = stringDiff
Expand All @@ -46,6 +46,6 @@ export function buildNotEqualErrorMessage<T>(
const actualString = String(actual);
const expectedString = String(expected);

const msgSuffix = msg ? `: ${msg}` : ".";
return `Expected actual: ${actualString} not to be: ${expectedString}${msgSuffix}`;
const msgPrefix = msg ? `${msg}: ` : "";
return `${msgPrefix}Expected actual: ${actualString} not to be: ${expectedString}.`;
}
Loading
Loading