TypeScript 和 chai-as-promsied:最终无效 属性
TypeScript and chai-as-promsied: eventually is an invalid property
我正在尝试使用 TypScript 编写我的 Cucumber 测试,如下所示:
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);
});
然而,量角器抱怨:
Error: Invalid Chai property: eventually
我如何导入这个?我试过:
import { eventual } from 'chai-as-promised';
但这不起作用。我怎样才能做到这一点?我也曾尝试使用 await
重写 Then
调用,但编译器抱怨您不能将回调与异步函数混合使用。啊!
在量角器配置中,在 onPrepare
函数的末尾添加以下行:
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();
}
使用异步函数时,您应该从函数签名中删除回调。
Then('I want to see the welcome message {string}',
async function (message) {
await chai.expect($$('h1').first().getText())
.to.eventually.equal(message);
});
我正在尝试使用 TypScript 编写我的 Cucumber 测试,如下所示:
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);
});
然而,量角器抱怨:
Error: Invalid Chai property: eventually
我如何导入这个?我试过:
import { eventual } from 'chai-as-promised';
但这不起作用。我怎样才能做到这一点?我也曾尝试使用 await
重写 Then
调用,但编译器抱怨您不能将回调与异步函数混合使用。啊!
在量角器配置中,在 onPrepare
函数的末尾添加以下行:
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();
}
使用异步函数时,您应该从函数签名中删除回调。
Then('I want to see the welcome message {string}',
async function (message) {
await chai.expect($$('h1').first().getText())
.to.eventually.equal(message);
});