1 回答
TA贡献1790条经验 获得超9个赞
首先加载您的自定义插件,然后添加chai-as-promise
. 与加载插件的顺序有关。
来自#installation-and-setup
使用其他 Chai 插件时请注意:Chai as Promised 会在安装时查找所有当前注册的断言器并承诺它们。因此,如果您希望其断言器被 Promisified,则应在任何其他 Chai 插件之后将 Chai 安装为 Promised last 。
例如
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(function(chai, utils) {
var Assertion = chai.Assertion;
Assertion.addMethod('convertToStringEqual', function(input) {
new Assertion(this._obj.toString()).to.equal(input.toString());
});
});
chai.use(chaiAsPromised);
chai.should();
describe('65418901', () => {
it('should pass', () => {
return Promise.resolve(2 + 2).should.eventually.convertToStringEqual(4);
});
});
单元测试结果:
65418901
✓ should pass
1 passing (52ms)
但是,像这样加载插件将不起作用:
chai.use(chaiAsPromised);
chai.use(function(chai, utils) {
var Assertion = chai.Assertion;
Assertion.addMethod('convertToStringEqual', function(input) {
new Assertion(this._obj.toString()).to.equal(input.toString());
});
});
chai.should();
添加回答
举报