diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index ce72149984..18cf9134e0 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -2,7 +2,7 @@ /** This module is browser compatible. Do not rely on good formatting of values * for AssertionError messages in browsers. */ -import { red, green, white, gray, bold } from "../fmt/colors.ts"; +import { red, green, white, gray, bold, stripColor } from "../fmt/colors.ts"; import diff, { DiffType, DiffResult } from "./diff.ts"; const CAN_NOT_DISPLAY = "[Cannot display]"; @@ -289,9 +289,9 @@ export function assertArrayContains( return; } if (!msg) { - msg = `actual: "${actual}" expected to contain: "${expected}"`; - msg += "\n"; - msg += `missing: ${missing}`; + msg = `actual: "${format(actual)}" expected to contain: "${format( + expected + )}"\nmissing: ${format(missing)}`; } throw new AssertionError(msg); } @@ -342,7 +342,10 @@ export function assertThrows( }"${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg); } - if (msgIncludes && !e.message.includes(msgIncludes)) { + if ( + msgIncludes && + !stripColor(e.message).includes(stripColor(msgIncludes)) + ) { msg = `Expected error message to include "${msgIncludes}", but got "${ e.message }"${msg ? `: ${msg}` : "."}`; @@ -375,7 +378,10 @@ export async function assertThrowsAsync( }"${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg); } - if (msgIncludes && !e.message.includes(msgIncludes)) { + if ( + msgIncludes && + !stripColor(e.message).includes(stripColor(msgIncludes)) + ) { msg = `Expected error message to include "${msgIncludes}", but got "${ e.message }"${msg ? `: ${msg}` : "."}`; diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index fb25d46cf7..c333d41dac 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -151,15 +151,11 @@ test("testingArrayContains", function (): void { const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; assertArrayContains(fixture, ["deno"]); assertArrayContains(fixtureObject, [{ deno: "luv" }]); - let didThrow; - try { - assertArrayContains(fixtureObject, [{ deno: "node" }]); - didThrow = false; - } catch (e) { - assert(e instanceof AssertionError); - didThrow = true; - } - assertEquals(didThrow, true); + assertThrows( + (): void => assertArrayContains(fixtureObject, [{ deno: "node" }]), + AssertionError, + `actual: "[ { deno: "luv" }, { deno: "Js" } ]" expected to contain: "[ { deno: "node" } ]"\nmissing: [ { deno: "node" } ]` + ); }); test("testingAssertStringContainsThrow", function (): void {