From cce75f469a78eb5fd86057bba74b486aa0a81f13 Mon Sep 17 00:00:00 2001 From: Jon Short Date: Thu, 4 Jun 2020 15:43:05 +0100 Subject: [PATCH] feat(testing): Allow non-void promises in assertThrowsAsync (denoland/deno#6052) --- testing/asserts.ts | 8 ++++---- testing/asserts_test.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/testing/asserts.ts b/testing/asserts.ts index 18cf9134e05a..5f5c3a7c5eeb 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -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( + fn: () => T, ErrorClass?: Constructor, msgIncludes = "", msg?: string @@ -361,8 +361,8 @@ export function assertThrows( return error; } -export async function assertThrowsAsync( - fn: () => Promise, +export async function assertThrowsAsync( + fn: () => Promise, ErrorClass?: Constructor, msgIncludes = "", msg?: string diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index c333d41dac7e..3969cd661b80 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -9,6 +9,7 @@ import { assertEquals, assertStrictEq, assertThrows, + assertThrowsAsync, AssertionError, equal, fail, @@ -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"); + }); +}); + const createHeader = (): string[] => [ "", "",