summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorVincent LE GOFF <g_n_s@hotmail.fr>2019-03-05 02:03:50 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-03-04 20:03:50 -0500
commit39fde3a454b6bcc7daa6bca4fb7f4317550e9e58 (patch)
tree86f639a1536466423b5ba26b60ea04d59f2a970d /testing
parent17663c12326dd1053f89a3bd741807f139973dae (diff)
Use pretty assertEqual in testing (denoland/deno_std#234)
Original: https://github.com/denoland/deno_std/commit/8fb9f709a67e92634b0be3024dfbc70fb445883c
Diffstat (limited to 'testing')
-rw-r--r--testing/mod.ts30
-rw-r--r--testing/pretty.ts9
-rw-r--r--testing/test.ts7
3 files changed, 14 insertions, 32 deletions
diff --git a/testing/mod.ts b/testing/mod.ts
index c1bf2015b..0f6ffefbc 100644
--- a/testing/mod.ts
+++ b/testing/mod.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { green, red } from "../colors/mod.ts";
+import { assertEqual as prettyAssertEqual } from "./pretty.ts";
interface Constructor {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -46,30 +47,7 @@ const assertions = {
* deeply equal, then throw.
*/
equal(actual: unknown, expected: unknown, msg?: string): void {
- if (!equal(actual, expected)) {
- let actualString: string;
- let expectedString: string;
- try {
- actualString = String(actual);
- } catch (e) {
- actualString = "[Cannot display]";
- }
- try {
- expectedString = String(expected);
- } catch (e) {
- expectedString = "[Cannot display]";
- }
- console.error(
- "assertEqual failed. actual =",
- actualString,
- "expected =",
- expectedString
- );
- if (!msg) {
- msg = `actual: ${actualString} expected: ${expectedString}`;
- }
- throw new Error(msg);
- }
+ prettyAssertEqual(actual, expected, msg);
},
/** Make an assertion that `actual` and `expected` are strictly equal. If
@@ -187,10 +165,10 @@ Object.assign(assertions.assert, assertions);
export const assert = assertions.assert as Assert;
/**
- * An alias to assert.equal
+ * Alias to pretty.assertEqual
* @deprecated
*/
-export const assertEqual = assert.equal;
+export const assertEqual = prettyAssertEqual;
export type TestFunction = () => void | Promise<void>;
diff --git a/testing/pretty.ts b/testing/pretty.ts
index b27b3ccd7..737d9c6dd 100644
--- a/testing/pretty.ts
+++ b/testing/pretty.ts
@@ -55,7 +55,11 @@ function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>): string[] {
return messages;
}
-export function assertEqual(actual: unknown, expected: unknown): void {
+export function assertEqual(
+ actual: unknown,
+ expected: unknown,
+ msg?: string
+): void {
if (equal(actual, expected)) {
return;
}
@@ -71,5 +75,8 @@ export function assertEqual(actual: unknown, expected: unknown): void {
} catch (e) {
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
}
+ if (msg) {
+ message = msg;
+ }
throw new Error(message);
}
diff --git a/testing/test.ts b/testing/test.ts
index 7182a5783..a90ab5dc7 100644
--- a/testing/test.ts
+++ b/testing/test.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual, equal, runIfMain } from "./mod.ts";
+import { assertEqual as prettyAssertEqual } from "./pretty.ts";
import "./format_test.ts";
import "./diff_test.ts";
import "./pretty_test.ts";
@@ -31,7 +32,7 @@ test(function testingAssertEqual() {
const a = Object.create(null);
a.b = "foo";
assert.equal(a, a);
- assert(assertEqual === assert.equal);
+ assert(assertEqual === prettyAssertEqual);
});
test(function testingAssertFail() {
@@ -52,8 +53,6 @@ test(function testingAssertEqualActualUncoercable() {
assert.equal(a, "bar");
} catch (e) {
didThrow = true;
- console.log(e.message);
- assert(e.message === "actual: [Cannot display] expected: bar");
}
assert(didThrow);
});
@@ -65,8 +64,6 @@ test(function testingAssertEqualExpectedUncoercable() {
assert.equal("bar", a);
} catch (e) {
didThrow = true;
- console.log(e.message);
- assert(e.message === "actual: bar expected: [Cannot display]");
}
assert(didThrow);
});