Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-void promises in assertThrowsAsync #6052

Merged
merged 8 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions std/testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ export function fail(msg?: string): void {
* throws. An error class and a string that should be included in the
* error message can also be asserted.
*/
export function assertThrows(
fn: () => void,
export function assertThrows<T = void>(
fn: () => T,
ErrorClass?: Constructor,
msgIncludes = "",
msg?: string
Expand Down Expand Up @@ -361,8 +361,8 @@ export function assertThrows(
return error;
}

export async function assertThrowsAsync(
fn: () => Promise<void>,
export async function assertThrowsAsync<T = void>(
fn: () => Promise<T>,
ErrorClass?: Constructor,
msgIncludes = "",
msg?: string
Expand Down
15 changes: 15 additions & 0 deletions std/testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
assertEquals,
assertStrictEq,
assertThrows,
assertThrowsAsync,
AssertionError,
equal,
fail,
Expand Down Expand Up @@ -245,6 +246,20 @@ test("testingAssertFailWithWrongErrorClass", function (): void {
);
});

test("testingAssertThrowsWithReturnType", () => {
assertThrows(() => {
throw new Error();
return "a string";
});
});

test("testingAssertThrowsAsyncWithReturnType", () => {
assertThrowsAsync(() => {
throw new Error();
return Promise.resolve("a Promise<string>");
});
});

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