summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Waller <rdwaller1984@gmail.com>2020-07-14 19:41:05 +0100
committerGitHub <noreply@github.com>2020-07-14 14:41:05 -0400
commit9eca71caa1674c31f9cc5d4e86c03f10b59e0a00 (patch)
treec7d61aadc68479ab3c2889e1ac925a877bfcb349
parentfe8399973a5a1dd8a21cbb6edc88415feb83b2ef (diff)
fix(std/testing): assertThrows inheritance (#6623)
-rw-r--r--std/testing/asserts.ts4
-rw-r--r--std/testing/asserts_test.ts24
2 files changed, 24 insertions, 4 deletions
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index b1164090d..2a1358143 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -394,7 +394,7 @@ export function assertThrows<T = void>(
if (e instanceof Error === false) {
throw new AssertionError("A non-Error object was thrown.");
}
- if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
+ if (ErrorClass && !(e instanceof ErrorClass)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
e.constructor.name
}"${msg ? `: ${msg}` : "."}`;
@@ -438,7 +438,7 @@ export async function assertThrowsAsync<T = void>(
if (e instanceof Error === false) {
throw new AssertionError("A non-Error object was thrown or rejected.");
}
- if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
+ if (ErrorClass && !(e instanceof ErrorClass)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
e.name
}"${msg ? `: ${msg}` : "."}`;
diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts
index c7fa5a737..c3059ebac 100644
--- a/std/testing/asserts_test.ts
+++ b/std/testing/asserts_test.ts
@@ -275,12 +275,12 @@ Deno.test("testingAssertFailWithWrongErrorClass", function (): void {
(): void => {
fail("foo");
},
- Error,
+ TypeError,
"Failed assertion: foo"
);
},
AssertionError,
- `Expected error to be instance of "Error", but was "AssertionError"`
+ `Expected error to be instance of "TypeError", but was "AssertionError"`
);
});
@@ -626,3 +626,23 @@ Deno.test("assert diff formatting", () => {
]`
);
});
+
+Deno.test("Assert Throws Parent Error", () => {
+ assertThrows(
+ () => {
+ throw new AssertionError("Fail!");
+ },
+ Error,
+ "Fail!"
+ );
+});
+
+Deno.test("Assert Throws Async Parent Error", () => {
+ assertThrowsAsync(
+ () => {
+ throw new AssertionError("Fail!");
+ },
+ Error,
+ "Fail!"
+ );
+});