description and source-codefunction chaiAsPromised(chai, utils) {
"use strict";
var Assertion = chai.Assertion,
assertionPrototype = Assertion.prototype,
expect = chai.expect,
containPropertyDesc = Object.getOwnPropertyDescriptor(assertionPrototype, 'contain');
Object.defineProperty(assertionPrototype, 'contains', containPropertyDesc);
Object.defineProperty(assertionPrototype, 'includes', containPropertyDesc);
// Handles the `something` chain property
function chainSomething() {
// `include` or `contains` should have been called before
if (!utils.flag(this, "contains"))
throw new Error("cannot use something without include or contains");
// Flag that this is a `something` chain
var lastSomething = this, newSomething = {};
while (utils.flag(lastSomething, "something"))
lastSomething = utils.flag(lastSomething, "something");
// Transfer all the flags to the new `something` and remove them from `this`
utils.transferFlags(this, newSomething, false);
for (var prop in this.__flags)
if (!/^(?:something|object|ssfi|message)$/.test(prop))
delete this.__flags[prop];
// Add the `newSomething` to the `lastSomething` in the chain.
utils.flag(lastSomething, "something", newSomething);
// Clear the `something` flag from `newSomething`.
utils.flag(newSomething, "something", false);
}
// Performs the `something()` assertion
function assertSomething() {
// Undo the flags set by the `something` chain property
var somethingFlags = utils.flag(this, "something");
utils.flag(this, "something", false);
if (somethingFlags)
utils.transferFlags(somethingFlags, this, true);
// The assertion's object for `something` should be array-like
var object = utils.flag(this, "object");
expect(object).to.have.property("length");
expect(object.length).to.be.a("number", "something object length");
// The object should contain something
this.assert(object.length > 0,
"expected #{this} to contain something",
"expected #{this} not to contain something"
);
}
// Handles the `all` chain property
function chainAll() {
// Flag that this is an `all` chain
var lastAll = this, newAll = {};
while (utils.flag(lastAll, "all"))
lastAll = utils.flag(lastAll, "all");
// Transfer all the flags to the new `all` and remove them from `this`
utils.transferFlags(this, newAll, false);
for (var prop in this.__flags)
if (!/^(?:all|object|ssfi|message)$/.test(prop))
delete this.__flags[prop];
// Add the `newAll` to the `lastAll` in the chain.
utils.flag(lastAll, "all", newAll);
// Clear the `all` flag from `newAll`.
utils.flag(newAll, "all", false);
}
// Find all assertion methods
var methodNames = Object.getOwnPropertyNames(assertionPrototype)
.filter(function (propertyName) {
var property = Object.getOwnPropertyDescriptor(assertionPrototype, propertyName);
return typeof property.value === "function";
});
// Override all assertion methods
methodNames.forEach(function (methodName) {
// Override the method to react on a possible `something` in the chain
Assertion.overwriteMethod(methodName, function (_super) {
return function somethingMethod() {
// Return if no `something` has been used in the chain
var somethingFlags = utils.flag(this, "something");
if (!somethingFlags)
return _super.apply(this, arguments);
// Use the nested `something` flag as the new `something` flag for this.
utils.flag(this, "something", utils.flag(somethingFlags, "something"));
// The assertion's object for `something` should be array-like
var arrayObject = utils.flag(this, "object");
expect(arrayObject).to.have.property("length");
var length = arrayObject.length;
expect(length).to.be.a("number", "something object length");
// In the negative case, an empty array means success
var negate = utils.flag(somethingFlags, "negate");
if (negate && ...