summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent LE GOFF <g_n_s@hotmail.fr>2019-03-08 22:04:43 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-03-08 16:04:43 -0500
commit831d74e364b634eab4f587f30570e17c8c54c17e (patch)
treebe309ef55902e552fdd6ad2403c208096e9f4fde
parent3761d41d32d6e6baffa56adbda380d04ad10e710 (diff)
Use AssertionError instead of Error in testing (denoland/deno_std#254)
Original: https://github.com/denoland/deno_std/commit/d4088a1920d005132bce2b0cd948ed74e0250563
-rw-r--r--testing/asserts.ts55
-rw-r--r--testing/asserts_test.ts27
-rw-r--r--testing/test.ts14
3 files changed, 52 insertions, 44 deletions
diff --git a/testing/asserts.ts b/testing/asserts.ts
index 6431a11a5..33d6073ea 100644
--- a/testing/asserts.ts
+++ b/testing/asserts.ts
@@ -6,6 +6,13 @@ interface Constructor {
new (...args: any[]): any;
}
+export class AssertionError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = "AssertionError";
+ }
+}
+
export function equal(c: unknown, d: unknown): boolean {
const seen = new Map();
return (function compare(a: unknown, b: unknown) {
@@ -36,7 +43,7 @@ export function equal(c: unknown, d: unknown): boolean {
/** Make an assertion, if not `true`, then throw. */
export function assert(expr: boolean, msg = ""): void {
if (!expr) {
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
@@ -85,7 +92,7 @@ export function assertNotEquals(
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
- throw new Error(msg);
+ throw new AssertionError(msg);
}
/**
@@ -119,7 +126,7 @@ export function assertStrictEq(
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
@@ -142,7 +149,7 @@ export function assertStrContains(
if (!msg) {
msg = `actual: "${actual}" expected to contains: "${expected}"`;
}
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
@@ -182,7 +189,7 @@ export function assertArrayContains(
msg += "\n";
msg += `missing: ${missing}`;
}
- throw new Error(msg);
+ throw new AssertionError(msg);
}
/**
@@ -204,7 +211,7 @@ export function assertMatch(
if (!msg) {
msg = `actual: "${actual}" expected to match: "${expected}"`;
}
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
@@ -234,21 +241,19 @@ export function assertThrows(
msg = `Expected error to be instance of "${ErrorClass.name}"${
msg ? `: ${msg}` : "."
}`;
- throw new Error(msg);
+ throw new AssertionError(msg);
}
- if (msgIncludes) {
- if (!e.message.includes(msgIncludes)) {
- msg = `Expected error message to include "${msgIncludes}", but got "${
- e.message
- }"${msg ? `: ${msg}` : "."}`;
- throw new Error(msg);
- }
+ if (msgIncludes && !e.message.includes(msgIncludes)) {
+ msg = `Expected error message to include "${msgIncludes}", but got "${
+ e.message
+ }"${msg ? `: ${msg}` : "."}`;
+ throw new AssertionError(msg);
}
doesThrow = true;
}
if (!doesThrow) {
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
@@ -266,30 +271,28 @@ export async function assertThrowsAsync(
msg = `Expected error to be instance of "${ErrorClass.name}"${
msg ? `: ${msg}` : "."
}`;
- throw new Error(msg);
+ throw new AssertionError(msg);
}
- if (msgIncludes) {
- if (!e.message.includes(msgIncludes)) {
- msg = `Expected error message to include "${msgIncludes}", but got "${
- e.message
- }"${msg ? `: ${msg}` : "."}`;
- throw new Error(msg);
- }
+ if (msgIncludes && !e.message.includes(msgIncludes)) {
+ msg = `Expected error message to include "${msgIncludes}", but got "${
+ e.message
+ }"${msg ? `: ${msg}` : "."}`;
+ throw new AssertionError(msg);
}
doesThrow = true;
}
if (!doesThrow) {
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
- throw new Error(msg);
+ throw new AssertionError(msg);
}
}
/** Use this to stub out methods that will throw when invoked. */
export function unimplemented(msg?: string): never {
- throw new Error(msg || "unimplemented");
+ throw new AssertionError(msg || "unimplemented");
}
/** Use this to assert unreachable code. */
export function unreachable(): never {
- throw new Error("unreachable");
+ throw new AssertionError("unreachable");
}
diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts
index ccec2af09..6f82d4169 100644
--- a/testing/asserts_test.ts
+++ b/testing/asserts_test.ts
@@ -2,20 +2,19 @@
import {
assert,
- equal,
assertNotEquals,
assertStrContains,
assertArrayContains,
assertMatch,
assertEquals,
+ assertThrows,
+ AssertionError,
+ equal,
+ fail,
unimplemented,
unreachable
} from "./asserts.ts";
import { test } from "./mod.ts";
-// import { assertEquals as prettyAssertEqual } from "./pretty.ts";
-// import "./format_test.ts";
-// import "./diff_test.ts";
-// import "./pretty_test.ts";
test(function testingEqual() {
assert(equal("world", "world"));
@@ -49,6 +48,7 @@ test(function testingNotEquals() {
assertNotEquals("Raptor", "Raptor");
didThrow = false;
} catch (e) {
+ assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@@ -63,6 +63,7 @@ test(function testingAssertStringContains() {
assertStrContains("Denosaurus", "Raptor");
didThrow = false;
} catch (e) {
+ assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@@ -78,6 +79,7 @@ test(function testingArrayContains() {
assertArrayContains(fixtureObject, [{ deno: "node" }]);
didThrow = false;
} catch (e) {
+ assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@@ -92,6 +94,7 @@ test(function testingAssertStringContainsThrow() {
e.message ===
`actual: "Denosaurus from Jurassic" expected to contains: "Raptor"`
);
+ assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@@ -110,6 +113,7 @@ test(function testingAssertStringMatchingThrows() {
e.message ===
`actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"`
);
+ assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@@ -121,6 +125,7 @@ test(function testingAssertsUnimplemented() {
unimplemented();
} catch (e) {
assert(e.message === "unimplemented");
+ assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@@ -132,7 +137,19 @@ test(function testingAssertsUnreachable() {
unreachable();
} catch (e) {
assert(e.message === "unreachable");
+ assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
});
+
+test(function testingAssertFail() {
+ assertThrows(fail, AssertionError, "Failed assertion.");
+ assertThrows(
+ () => {
+ fail("foo");
+ },
+ AssertionError,
+ "Failed assertion: foo"
+ );
+});
diff --git a/testing/test.ts b/testing/test.ts
index 7f904346c..367e28022 100644
--- a/testing/test.ts
+++ b/testing/test.ts
@@ -5,25 +5,13 @@ import {
assertEquals,
assertStrictEq,
assertThrows,
- assertThrowsAsync,
- fail
+ assertThrowsAsync
} from "../testing/asserts.ts";
import "./format_test.ts";
import "./diff_test.ts";
import "./pretty_test.ts";
import "./asserts_test.ts";
-test(function testingAssertFail() {
- assertThrows(fail, Error, "Failed assertion.");
- assertThrows(
- () => {
- fail("foo");
- },
- Error,
- "Failed assertion: foo"
- );
-});
-
test(function testingAssertEqualActualUncoercable() {
let didThrow = false;
const a = Object.create(null);