summaryrefslogtreecommitdiff
path: root/testing/asserts.ts
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 /testing/asserts.ts
parent3761d41d32d6e6baffa56adbda380d04ad10e710 (diff)
Use AssertionError instead of Error in testing (denoland/deno_std#254)
Original: https://github.com/denoland/deno_std/commit/d4088a1920d005132bce2b0cd948ed74e0250563
Diffstat (limited to 'testing/asserts.ts')
-rw-r--r--testing/asserts.ts55
1 files changed, 29 insertions, 26 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");
}