assertthat
Version:
assertthat provides fluent TDD.
49 lines (48 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertFunctionIsNotThrowingAsync = void 0;
const errors_1 = require("../../errors");
const defekt_1 = require("defekt");
const assertFunctionIsNotThrowingAsync = 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.error)(new errors_1.AssertionFailed({
message: 'The function threw an unexpected asynchronous exception.',
actual: `Error message:\n${ex.message}`
}));
}
if (expected instanceof RegExp && expected.test(ex.message)) {
return (0, defekt_1.error)(new errors_1.AssertionFailed({
message: 'The function threw an unexpected asynchronous exception.',
expected: `The message should not have matched:\n${expected.toString()}`,
actual: `Error message:\n${ex.message}`
}));
}
if (typeof expected === 'function' && expected(ex)) {
return (0, defekt_1.error)(new errors_1.AssertionFailed({
message: 'The function threw an unexpected asynchronous exception.',
expected: `The exception should not have fulfilled a predicate.`,
actual: `Error message:\n${ex.message}`
}));
}
if (typeof expected === 'string' && ex.message === expected) {
return (0, defekt_1.error)(new errors_1.AssertionFailed({
message: 'The function threw an unexpected asynchronous exception.',
expected: `The message should not have been:\n${expected}`,
actual: `Error message:\n${ex.message}`
}));
}
}
return (0, defekt_1.value)();
};
exports.assertFunctionIsNotThrowingAsync = assertFunctionIsNotThrowingAsync;