UNPKG

chai-soft-assert

Version:

Chai helper to include soft assertions on chai package

117 lines (116 loc) 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.softAssertion = exports.softAssertionsCollector = 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"]; class SoftAssertionsCollector { constructor() { this.errors = []; } add(error) { this.errors.push(error); } assertAll() { if (this.errors.length > 0) { const message = this.errors .map((e, i) => `#${i + 1} [${e.matcher}]: ${e.message}` + (e.actual !== undefined && e.expected !== undefined ? `\n Actual: ${JSON.stringify(e.actual)}\n Expected: ${JSON.stringify(e.expected)}` : "")) .join("\n\n"); throw new Error(`Soft assertion failures:\n\n${message}`); } } clear() { this.errors = []; } } exports.softAssertionsCollector = new SoftAssertionsCollector(); 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) { exports.softAssertionsCollector.add({ matcher: method, message: error.message, 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) { exports.softAssertionsCollector.add({ matcher: method, message: error.message, actual: error.actual, expected: value, }); } } else { _super.apply(this, arguments); } }; }, function (_super) { return function () { _super.apply(this, arguments); }; }); }); }; } exports.softAssertion = createSoftAssertion(softMethods, softChainableMethods);