Reputation: 296
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?
Upvotes: 4
Views: 12580
Reputation: 21698
I would say to use async and await to handle promise object with minimal code changes.
// no changes to page.po.ts. handle async and await in .spec.ts file
import {browser, by, element} from 'protractor';
export class HomePage {
navigateTo() {
return browser.get('/');
}
getParagraphText() {
return element(by.css('cfs-root h1')).getText();
}
}
//page.e2e-spec.ts
import {HomePage} from './home.po';
describe('ng-app App', () => {
let page: HomePage;
beforeEach(() => {
page = new HomePage();
});
it('should display the page heading.', async () => { // notice the callback is a async function
page.navigateTo();
expect(await page.getParagraphText()).toBe('cfs works!'); //here we are using await to handle the promise
page.getHomeSearchHeading().then((text) => {
expect(text).toEqual('Universal Data Catalog');
});
});
});
Upvotes: 1
Reputation: 29864
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)
Upvotes: 7
Reputation: 164297
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);
});
});
Upvotes: 6