summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Waller <rdwaller1984@gmail.com>2020-06-23 15:47:09 +0100
committerGitHub <noreply@github.com>2020-06-23 10:47:09 -0400
commitd615f0ff82f5b2b530940a21a61e5b4b08b1d1f4 (patch)
tree2a5799e1db71db8a5a9efd5e7ab6e9a14dfde3df
parentd2403caa3b009c1db0dbbf172a03983ca1355d70 (diff)
fix(std/testing): assertThrows gracefully fails if non-Error thrown (#6330)
-rw-r--r--std/testing/asserts.ts14
-rw-r--r--std/testing/asserts_test.ts83
2 files changed, 96 insertions, 1 deletions
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index 50a9c59ce..50c45c6ca 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -322,7 +322,8 @@ export function fail(msg?: string): void {
assert(false, `Failed assertion${msg ? `: ${msg}` : "."}`);
}
-/** Executes a function, expecting it to throw. If it does not, then it
+/**
+ * Executes a function, expecting it to throw. If it does not, then it
* throws. An error class and a string that should be included in the
* error message can also be asserted.
*/
@@ -337,6 +338,9 @@ export function assertThrows<T = void>(
try {
fn();
} catch (e) {
+ if (e instanceof Error === false) {
+ throw new AssertionError("A non-Error object was thrown.");
+ }
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
e.constructor.name
@@ -362,6 +366,11 @@ export function assertThrows<T = void>(
return error;
}
+/**
+ * Executes a function which returns a promise, expecting it to throw or reject.
+ * If it does not, then it throws. An error class and a string that should be
+ * included in the error message can also be asserted.
+ */
export async function assertThrowsAsync<T = void>(
fn: () => Promise<T>,
ErrorClass?: Constructor,
@@ -373,6 +382,9 @@ export async function assertThrowsAsync<T = void>(
try {
await fn();
} catch (e) {
+ if (e instanceof Error === false) {
+ throw new AssertionError("A non-Error object was thrown or rejected.");
+ }
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
e.name
diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts
index 36d054356..8b79ddf8a 100644
--- a/std/testing/asserts_test.ts
+++ b/std/testing/asserts_test.ts
@@ -409,3 +409,86 @@ Deno.test({
);
},
});
+
+Deno.test("Assert Throws Non-Error Fail", () => {
+ assertThrows(
+ () => {
+ assertThrows(
+ () => {
+ throw "Panic!";
+ },
+ String,
+ "Panic!"
+ );
+ },
+ AssertionError,
+ "A non-Error object was thrown."
+ );
+
+ assertThrows(
+ () => {
+ assertThrows(() => {
+ throw null;
+ });
+ },
+ AssertionError,
+ "A non-Error object was thrown."
+ );
+
+ assertThrows(
+ () => {
+ assertThrows(() => {
+ throw undefined;
+ });
+ },
+ AssertionError,
+ "A non-Error object was thrown."
+ );
+});
+
+Deno.test("Assert Throws Async Non-Error Fail", () => {
+ assertThrowsAsync(
+ () => {
+ return assertThrowsAsync(
+ () => {
+ return Promise.reject("Panic!");
+ },
+ String,
+ "Panic!"
+ );
+ },
+ AssertionError,
+ "A non-Error object was thrown or rejected."
+ );
+
+ assertThrowsAsync(
+ () => {
+ return assertThrowsAsync(() => {
+ return Promise.reject(null);
+ });
+ },
+ AssertionError,
+ "A non-Error object was thrown or rejected."
+ );
+
+ assertThrowsAsync(
+ () => {
+ return assertThrowsAsync(() => {
+ return Promise.reject(undefined);
+ });
+ },
+ AssertionError,
+ "A non-Error object was thrown or rejected."
+ );
+
+ assertThrowsAsync(
+ () => {
+ return assertThrowsAsync(() => {
+ throw undefined;
+ return Promise.resolve("Ok!");
+ });
+ },
+ AssertionError,
+ "A non-Error object was thrown or rejected."
+ );
+});