diff --git a/async/pool_test.ts b/async/pool_test.ts index d0406534f437..4b7a6eac2db0 100644 --- a/async/pool_test.ts +++ b/async/pool_test.ts @@ -32,13 +32,19 @@ Deno.test("[async] pooledMap errors", async function () { return n; } const mappedNumbers: number[] = []; - const error = await assertThrowsAsync(async () => { - for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) { - mappedNumbers.push(m); + let error = new AggregateError([]); + await assertThrowsAsync(async () => { + try { + for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) { + mappedNumbers.push(m); + } + } catch (e) { + error = e; + throw e; } - }, AggregateError) as AggregateError; + }, AggregateError); assertEquals(mappedNumbers, [3]); - assertEquals(error.errors.length, 2); - assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1"); - assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2"); + assertEquals(error?.errors.length, 2); + assertStringIncludes(error?.errors[0].stack, "Error: Bad number: 1"); + assertStringIncludes(error?.errors[1].stack, "Error: Bad number: 2"); }); diff --git a/encoding/csv_test.ts b/encoding/csv_test.ts index 295b30b985b2..004aee4fb813 100644 --- a/encoding/csv_test.ts +++ b/encoding/csv_test.ts @@ -472,14 +472,20 @@ for (const t of testCases) { } let actual; if (t.Error) { - const err = await assertThrowsAsync(async () => { - await readMatrix(new BufReader(new StringReader(t.Input ?? "")), { - separator, - comment: comment, - trimLeadingSpace: trim, - fieldsPerRecord: fieldsPerRec, - lazyQuotes: lazyquote, - }); + let err; + await assertThrowsAsync(async () => { + try { + await readMatrix(new BufReader(new StringReader(t.Input ?? "")), { + separator, + comment: comment, + trimLeadingSpace: trim, + fieldsPerRecord: fieldsPerRec, + lazyQuotes: lazyquote, + }); + } catch (e) { + err = e; + throw e; + } }); assertEquals(err, t.Error); diff --git a/testing/README.md b/testing/README.md index 50d53bf40208..a730b8773143 100644 --- a/testing/README.md +++ b/testing/README.md @@ -33,7 +33,9 @@ pretty-printed diff of failing assertion. string. - `assertThrowsAsync()` - Expects the passed `fn` to be async and throw (or return a `Promise` that rejects). If the `fn` does not throw or reject, this - function will throw asynchronously. Also compares any errors thrown to an + function will throw asynchronously _(⚠️ assertion should be awaited or be the + return value of the test function to avoid any uncaught rejections which could + result in unexpected process exit)_. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string. - `unimplemented()` - Use this to stub out methods that will throw when invoked. diff --git a/testing/asserts.ts b/testing/asserts.ts index fb96a08dacff..d4d110fdafa7 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -584,9 +584,8 @@ export function assertThrows( ErrorClass?: Constructor, msgIncludes = "", msg?: string, -): Error { +): void { let doesThrow = false; - let error = null; try { fn(); } catch (e) { @@ -611,13 +610,11 @@ export function assertThrows( throw new AssertionError(msg); } doesThrow = true; - error = e; } if (!doesThrow) { msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg); } - return error; } /** @@ -630,9 +627,8 @@ export async function assertThrowsAsync( ErrorClass?: Constructor, msgIncludes = "", msg?: string, -): Promise { +): Promise { let doesThrow = false; - let error = null; try { await fn(); } catch (e) { @@ -657,13 +653,11 @@ export async function assertThrowsAsync( throw new AssertionError(msg); } doesThrow = true; - error = e; } if (!doesThrow) { msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg); } - return error; } /** Use this to stub out methods that will throw when invoked. */