Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Error: Argument of type 'number' is not assignable to parameter of type 'Expected<Promise<number>>'

I have the following function which fails on execution:

it('should show three items', () => {
    const EXPECTED_NUMBER_OF_ITEMS: number = 3;
    const countOfTabElements: Promise<number> = page.listOfTabs.count();
    expect(countOfTabElements).toBe(EXPECTED_NUMBER_OF_ITEMS);
});

It throws the following Error when i execute it:

Argument of type 'number' is not assignable to parameter of type 'Expected>'. (2345)

Any ideas why?

like image 576
Carsten Hilber Avatar asked Dec 07 '22 19:12

Carsten Hilber


2 Answers

Alternatively to @Nitzan Tomer answer you should be able to use async/await (TS >= 2.1 for targeting ES5)

it('should show three items', async () => {
    const EXPECTED_NUMBER_OF_ITEMS: number = 3;
    const value = await page.listOfTabs.count();
    expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
});

(as a side note I believe you need a fairly recent version of Mocha to handle promise rejection correctly)

like image 68
Bruno Grieder Avatar answered Dec 11 '22 09:12

Bruno Grieder


Try:

it('should show three items', () => {
    const EXPECTED_NUMBER_OF_ITEMS: number = 3;
    page.listOfTabs.count().then(value => {
        expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
    });
});
like image 26
Nitzan Tomer Avatar answered Dec 11 '22 09:12

Nitzan Tomer