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

fix(testing): change assertThrows and assertThrowsAsync return type to void and Promise<void> #1052

Merged
merged 4 commits into from
Jul 28, 2021
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
20 changes: 13 additions & 7 deletions async/pool_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
22 changes: 14 additions & 8 deletions encoding/csv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 2 additions & 8 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,8 @@ export function assertThrows<T = void>(
ErrorClass?: Constructor,
msgIncludes = "",
msg?: string,
): Error {
): void {
let doesThrow = false;
let error = null;
try {
fn();
} catch (e) {
Expand All @@ -611,13 +610,11 @@ export function assertThrows<T = void>(
throw new AssertionError(msg);
}
doesThrow = true;
error = e;
}
if (!doesThrow) {
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
throw new AssertionError(msg);
}
return error;
}

/**
Expand All @@ -630,9 +627,8 @@ export async function assertThrowsAsync<T = void>(
ErrorClass?: Constructor,
msgIncludes = "",
msg?: string,
): Promise<Error> {
): Promise<void> {
let doesThrow = false;
let error = null;
try {
await fn();
} catch (e) {
Expand All @@ -657,13 +653,11 @@ export async function assertThrowsAsync<T = void>(
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. */
Expand Down