UNPKG

assertthat

Version:
55 lines (54 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertFunctionIsThrowingAsync = void 0; const errors_1 = require("../../errors"); const defekt_1 = require("defekt"); const assertFunctionIsThrowingAsync = async function (actual, expected) { try { const promise = actual(); if (!(promise instanceof Promise)) { return (0, defekt_1.error)(new errors_1.AssertionFailed({ message: 'The function did not return a Promise.' })); } await promise; // 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 asynchronous 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 asynchronous 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 asynchronous 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 asynchronous exception.' })); }; exports.assertFunctionIsThrowingAsync = assertFunctionIsThrowingAsync;