Skip to content

Flow type for AssertContext.throws should match typescript type #1442

@dancoates

Description

@dancoates

Description

The flow type for AssetContext.throws specifies that the return value is Promise<Error>:

type AssertContext = {
    ...
    throws: {
        (value: PromiseLike<mixed>, error?: ErrorValidator, message?: string): Promise<Error>;
        (value: () => mixed, error?: ErrorValidator, message?: string): Error;
    };
    ...
};

This is inconsistent with the typescript types:

export interface AssertContext {
    ...
    throws(value: PromiseLike<any>, error?: ErrorValidator, message?: string): Promise<any>;
    throws(value: () => void, error?: ErrorValidator, message?: string): any;
    ...
}

and causes issues with flow when your code throws something other than an Error object.

Test Source

//@flow
import test from 'ava';

class CustomError extends Error {
    customProperty: string;
    constructor(message: string, customValue: string) {
        super(message);
        this.customProperty = customValue;
    }
}

function throwsCustomError(): Promise<string> {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            reject(new CustomError('message', 'customValue'));
        }, 500);
    });
}

test('Test something', async (tt: AssertContext): Promise<void> => {
    const error = await tt.throws(throwsCustomError());
    tt.is(error.customProperty, 'customValue');
});

Error Message & Stack Trace

test.js:22
 22:     tt.is(error.customProperty, 'customValue');
                     ^^^^^^^^^^^^^^ property `customProperty`. Property not found in
 22:     tt.is(error.customProperty, 'customValue');
               ^^^^^ Error


Found 1 error
error Command failed with exit code 2.

Config

Copy the relevant section from package.json:

{
  "ava": {
    "babel": "inherit"
  }
}

Relevant Links

Minimal repro

I'll submit a PR in a min with a fix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions