UNPKG

assertthat

Version:
49 lines (48 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertFunctionIsThrowing = void 0; const errors_1 = require("../../errors"); const defekt_1 = require("defekt"); const assertFunctionIsThrowing = function (actual, expected) { try { actual(); // eslint-disable-next-line @typescript-eslint/no-implicit-any-catch } catch (ex) { if (expected === undefined) { return (0, defekt_1.value)(); } if (expected instanceof RegExp) { if (expected.test(ex.message)) { return (0, defekt_1.value)(); } return (0, defekt_1.error)(new errors_1.AssertionFailed({ message: 'The function threw an unexpected exception.', expected: `The message should have matched:\n${expected.toString()}`, actual: `Error message:\n${ex.message}` })); } if (typeof expected === 'function') { if (expected(ex)) { return (0, defekt_1.value)(); } return (0, defekt_1.error)(new errors_1.AssertionFailed({ message: 'The function threw an unexpected exception.', expected: `The exception should have fulfilled a predicate.`, actual: `Error message:\n${ex.message}` })); } if (ex.message === expected) { return (0, defekt_1.value)(); } return (0, defekt_1.error)(new errors_1.AssertionFailed({ message: 'The function threw an unexpected exception.', expected: `The message should have been:\n${expected}`, actual: `Error message:\n${ex.message}` })); } return (0, defekt_1.error)(new errors_1.AssertionFailed({ message: 'The function did not throw an exception.' })); }; exports.assertFunctionIsThrowing = assertFunctionIsThrowing;