e2ed
Version:
E2E testing framework over Playwright
34 lines (33 loc) • 1.21 kB
JavaScript
;
/* eslint-disable no-unsafe-finally */
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertFunctionThrows = void 0;
const error_1 = require("../error");
/**
* Asserts that a function throws an exception when called.
* If the function returns a promise, then asserts that this promise will be rejected.
*/
const assertFunctionThrows = (func, check, payload) => {
const errorParams = { check, code: func.toString(), payload };
let returnedValue = undefined;
let shouldThrowInFinally = true;
try {
const functionValue = func();
if (typeof functionValue?.then === 'function') {
shouldThrowInFinally = false;
returnedValue = functionValue.then(() => {
throw new error_1.E2edError('Promise from asserted function did not rejected', errorParams);
}, () => { });
}
}
catch {
shouldThrowInFinally = false;
}
finally {
if (shouldThrowInFinally) {
throw new error_1.E2edError('Asserted function did not throw an exception', errorParams);
}
return returnedValue;
}
};
exports.assertFunctionThrows = assertFunctionThrows;