0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -05:00

fix(std/testing): assertThrows inheritance (#6623)

This commit is contained in:
Rob Waller 2020-07-14 19:41:05 +01:00 committed by GitHub
parent fe8399973a
commit 9eca71caa1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -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}` : "."}`;

View file

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