chai-soft-assert
Version:
Chai helper to include soft assertions on chai package
88 lines (87 loc) • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.softAssertion = void 0;
exports.addSoftMethod = addSoftMethod;
exports.getSoftMethods = getSoftMethods;
exports.addSoftChainableMethod = addSoftChainableMethod;
exports.getSoftChainableMethods = getSoftChainableMethods;
exports.createSoftAssertion = createSoftAssertion;
/* eslint-disable prefer-rest-params */
const softMethods = ["equal", "members", "property", "above", "below", "keys"];
const softChainableMethods = ["include", "contain", "a", "an", "lengthOf"];
function addSoftMethod(softMethod) {
if (Array.isArray(softMethod)) {
softMethods.push(...softMethod);
}
else {
softMethods.push(softMethod);
}
}
function getSoftMethods() {
return softMethods;
}
function addSoftChainableMethod(softChainableMethod) {
if (Array.isArray(softChainableMethod)) {
softChainableMethods.push(...softChainableMethod);
}
else {
softChainableMethods.push(softChainableMethod);
}
}
function getSoftChainableMethods() {
return softChainableMethods;
}
const flagName = "soft";
function createSoftAssertion(newSoftMethods = softMethods, newSoftChainableMethods = softChainableMethods) {
return function chaiSoftAssertion(chai, utils) {
//Creates flag to identify when soft asssertion is used
utils.addProperty(chai.Assertion.prototype, "soft", function () {
utils.flag(this, flagName, true);
});
//Uses an array of methods to overwrite them including soft assertion logic
newSoftMethods.forEach(function (method) {
utils.overwriteMethod(chai.Assertion.prototype, method, function (_super) {
return function (value) {
if (utils.flag(this, flagName)) {
try {
_super.apply(this, arguments);
}
catch (error) {
console.log("Soft error on", method, " assertion");
console.log("Message:", error.message);
console.log("Actual:", error.actual, "Expected:", value);
}
}
else {
_super.apply(this, arguments);
}
};
});
});
//Uses an array of chainable methods to overwrite them including soft assertion logic
newSoftChainableMethods.forEach(function (method) {
utils.overwriteChainableMethod(chai.Assertion.prototype, method, function (_super) {
return function (value) {
if (utils.flag(this, flagName)) {
try {
_super.apply(this, arguments);
}
catch (error) {
console.log("Soft error on", method, " assertion");
console.log("Message:", error.message);
console.log("Actual:", error.actual, "Expected:", value);
}
}
else {
_super.apply(this, arguments);
}
};
}, function (_super) {
return function () {
_super.apply(this, arguments);
};
});
});
};
}
exports.softAssertion = createSoftAssertion(softMethods, softChainableMethods);