Skip to content

Commit

Permalink
fix(std/testing): assertThrows inheritance (denoland#6623)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDWaller committed Jul 14, 2020
1 parent fe83999 commit 9eca71c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions std/testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}` : "."}`;
Expand Down Expand Up @@ -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}` : "."}`;
Expand Down
24 changes: 22 additions & 2 deletions std/testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
);
});

Expand Down Expand Up @@ -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!"
);
});

0 comments on commit 9eca71c

Please sign in to comment.