kizu
Version:
An easy-to-use, fast, and defensive Typescript/Javascript test runner designed to help you to write simple, readable, and maintainable tests.
49 lines • 2.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isError = isError;
exports.createDiagnosticRegexMismatch = createDiagnosticRegexMismatch;
const serialize_error_1 = require("serialize-error");
const equal_1 = require("./equal");
const pass_1 = require("./pass");
const AssertionError_1 = require("./AssertionError");
const kleur_1 = __importDefault(require("kleur"));
function isError(assertions, actualErr, expectedErr, description) {
if (!(actualErr instanceof Error))
throw new Error('actualErr is not an instance of Error');
if (!(expectedErr instanceof Error) && !(expectedErr instanceof RegExp))
throw new Error('expectedErr is not an instance of Error or a RegExp');
if (expectedErr instanceof RegExp) {
if (!expectedErr.test(actualErr.message)) {
const diagnostic = createDiagnosticRegexMismatch(actualErr.message, expectedErr);
const stack = new AssertionError_1.AssertionError('Expected error message does not match').stack;
assertions.push({ pass: false, description: description !== null && description !== void 0 ? description : 'errorsEquivalent()', diagnostic, stack });
}
else {
(0, pass_1.pass)(assertions, description !== null && description !== void 0 ? description : 'errorsEquivalent()');
}
}
else {
compareErrorObjects(assertions, actualErr, expectedErr, description);
}
}
function compareErrorObjects(assertions, actualErr, expectedErr, description) {
if (!(actualErr instanceof Error))
throw new Error('actualErr is not an instance of Error');
if (!(expectedErr instanceof Error) && !(expectedErr instanceof RegExp))
throw new Error('expectedErr is not an instance of Error or a RegExp');
const actualErrSerialized = (0, serialize_error_1.serializeError)(actualErr);
const expectedErrSerialized = (0, serialize_error_1.serializeError)(expectedErr);
// ignore stack traces
actualErrSerialized === null || actualErrSerialized === void 0 ? true : delete actualErrSerialized.stack;
expectedErrSerialized === null || expectedErrSerialized === void 0 ? true : delete expectedErrSerialized.stack;
(0, equal_1.equal)(assertions, actualErrSerialized, expectedErrSerialized, description || 'errorsEquivalent()');
}
function createDiagnosticRegexMismatch(actualErrMsg, expectedRegEx) {
const actual = `${kleur_1.default.grey().bold('Actual Error Message:')}\n\n${actualErrMsg}`;
const expected = `\n\n${kleur_1.default.grey().bold('Expected RegEx:')}\n\n${expectedRegEx}`;
return `${actual}${expected}\n\n`;
}
//# sourceMappingURL=isError.js.map