1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

fix(std/testing/asserts): Format values in assertArrayContains() (#6060)

This commit is contained in:
Nayeem Rahman 2020-06-03 04:38:46 +01:00 committed by GitHub
parent 1db98f10b8
commit aaa2ed5a64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 15 deletions

View file

@ -2,7 +2,7 @@
/** This module is browser compatible. Do not rely on good formatting of values /** This module is browser compatible. Do not rely on good formatting of values
* for AssertionError messages in browsers. */ * 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"; import diff, { DiffType, DiffResult } from "./diff.ts";
const CAN_NOT_DISPLAY = "[Cannot display]"; const CAN_NOT_DISPLAY = "[Cannot display]";
@ -289,9 +289,9 @@ export function assertArrayContains(
return; return;
} }
if (!msg) { if (!msg) {
msg = `actual: "${actual}" expected to contain: "${expected}"`; msg = `actual: "${format(actual)}" expected to contain: "${format(
msg += "\n"; expected
msg += `missing: ${missing}`; )}"\nmissing: ${format(missing)}`;
} }
throw new AssertionError(msg); throw new AssertionError(msg);
} }
@ -342,7 +342,10 @@ export function assertThrows(
}"${msg ? `: ${msg}` : "."}`; }"${msg ? `: ${msg}` : "."}`;
throw new AssertionError(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 "${ msg = `Expected error message to include "${msgIncludes}", but got "${
e.message e.message
}"${msg ? `: ${msg}` : "."}`; }"${msg ? `: ${msg}` : "."}`;
@ -375,7 +378,10 @@ export async function assertThrowsAsync(
}"${msg ? `: ${msg}` : "."}`; }"${msg ? `: ${msg}` : "."}`;
throw new AssertionError(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 "${ msg = `Expected error message to include "${msgIncludes}", but got "${
e.message e.message
}"${msg ? `: ${msg}` : "."}`; }"${msg ? `: ${msg}` : "."}`;

View file

@ -151,15 +151,11 @@ test("testingArrayContains", function (): void {
const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; const fixtureObject = [{ deno: "luv" }, { deno: "Js" }];
assertArrayContains(fixture, ["deno"]); assertArrayContains(fixture, ["deno"]);
assertArrayContains(fixtureObject, [{ deno: "luv" }]); assertArrayContains(fixtureObject, [{ deno: "luv" }]);
let didThrow; assertThrows(
try { (): void => assertArrayContains(fixtureObject, [{ deno: "node" }]),
assertArrayContains(fixtureObject, [{ deno: "node" }]); AssertionError,
didThrow = false; `actual: "[ { deno: "luv" }, { deno: "Js" } ]" expected to contain: "[ { deno: "node" } ]"\nmissing: [ { deno: "node" } ]`
} catch (e) { );
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
}); });
test("testingAssertStringContainsThrow", function (): void { test("testingAssertStringContainsThrow", function (): void {