serlingpa
serlingpa

Reputation: 12710

TypeScript and chai-as-promsied: eventually is an invalid property

I am trying to write my Cucumber tests using TypScript, like this:

import { browser, $$ } from 'protractor';
import { Given, Then } from 'cucumber'
import { expect } from 'chai';

Given('I navigate to the homepage', function (callback) {
  browser.get('http://localhost:4200');
  callback();
});

Then('I want to see the welcome message {string}', function (message, callback) {
  expect($$('h1').first().getText()).to.eventually.equal(message).and.notify(callback);
});

However, Protractor complains:

Error: Invalid Chai property: eventually

How can I import this? I have tried:

import { eventual } from 'chai-as-promised';

but this doesn't work. How can I accomplish this? I have also tried rewriting the Then call using await, but the compiler complains that you cannot mix callbacks with async functions. Aargh!

Upvotes: 8

Views: 5828

Answers (1)

ibenjelloun
ibenjelloun

Reputation: 7743

In your protractor configuration, add the following lines at the end of the onPrepare function :

onPrepare: function() {
 ...
 // Load chai assertions
 const chai = require('chai');
 const chaiAsPromised = require('chai-as-promised');

 // Load chai-as-promised support
 chai.use(chaiAsPromised);

 // Initialise should API (attaches as a property on Object)
 chai.should();
}

When using async function you should remove the callback from the function signature.

Then('I want to see the welcome message {string}',
async function (message) {
  await chai.expect($$('h1').first().getText())
.to.eventually.equal(message);
});

Upvotes: 10

Related Questions