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
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Deno.test("fails", function (): void {

Using `assertThrowsAsync()`:

> ⚠️ Note that it is _required_ to `await` for `assertThrowsAsync` to ensure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically isn't true. The promise returned from assertThrowsAsync can just be returned as well.

It would be more accurate to state that assertThrowsAsync returns a promise that will be resolved if the assertion passes, or rejected if it fails. This promise should be be awaited or be the return value from the test function.

Also, it would be far better to put it in the inline documentation for the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically isn't true. The promise returned from assertThrowsAsync can just be returned as well.
This promise should be be awaited or be the return value from the test function.

This is actually not possible unless you void it. Trying to return the promise directly will make TypeScript complain because the test function expects void | Promise<void>:

error: TS2322 [ERROR]: Type 'Promise<Error>' is not assignable to type 'void | Promise<void>'.
  Type 'Promise<Error>' is not assignable to type 'Promise<void>'.
    Type 'Error' is not assignable to type 'void'.
Deno.test("2", () => assertThrowsAsync(async () => {}, Error));
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at file:///deno_std/test.ts:6:22

    The expected type comes from the return type of this signature.
      export function test(name: string, fn: () => void | Promise<void>): void;
                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
        at asset:///lib.deno.ns.d.ts:188:42

It would be more accurate to state that assertThrowsAsync returns a promise that will be resolved if the assertion passes, or rejected if it fails. This promise should be be awaited or be the return value from the test function.

I'll change the wording to reflect the first part of your suggestion and update the inline documentation at the top.
The last part about being the return value from the test function is not valid though (at least currently)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertThrowsAsync shouldn't resolve with an error. I didn't realise that. Assertions shouldn't have return values.

Copy link
Contributor Author

@lowlighter lowlighter Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that both assertThrows and assertThrowsAsync actually had Error and Promise<Error> instead of void and Promise<void> as return type so I patched them.

Assertions shouldn't have return values.

I agree with that statement, but it does feel awkward to retrieve error class from within the test function now.
At least it makes them consistent with other assertions return type and make it possible to avoid brackets use from inline arrow functions

> that all promises get resolved within the test `Deno.test` callback. Failing
> to do so may result in early exit or triggering the Op sanitizer

```ts
Deno.test("doesThrow", async function () {
await assertThrowsAsync(
Expand Down