Skip to content

Commit

Permalink
test: add actual error class to fail message (denoland#4305)
Browse files Browse the repository at this point in the history
  • Loading branch information
cknight committed Mar 9, 2020
1 parent 7f591c3 commit 2115b38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions std/testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ export function assertThrows(
fn();
} catch (e) {
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
msg = `Expected error to be instance of "${ErrorClass.name}"${
msg ? `: ${msg}` : "."
}`;
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
e.constructor.name
}"${msg ? `: ${msg}` : "."}`;
throw new AssertionError(msg);
}
if (msgIncludes && !e.message.includes(msgIncludes)) {
Expand Down
18 changes: 18 additions & 0 deletions std/testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ test(function testingAssertFail(): void {
);
});

test(function testingAssertFailWithWrongErrorClass(): void {
assertThrows(
(): void => {
//This next assertThrows will throw an AssertionError due to the wrong
//expected error class
assertThrows(
(): void => {
fail("foo");
},
Error,
"Failed assertion: foo"
);
},
AssertionError,
`Expected error to be instance of "Error", but was "AssertionError"`
);
});

const createHeader = (): string[] => [
"",
"",
Expand Down

0 comments on commit 2115b38

Please sign in to comment.