UNPKG

e2ed

Version:

E2E testing framework over Playwright

48 lines (47 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPromiseWithResolveAndReject = void 0; const internal_1 = require("../../constants/internal"); const asserts_1 = require("../asserts"); const error_1 = require("../error"); const fn_1 = require("../fn"); const generalLog_1 = require("../generalLog"); const getDurationWithUnits_1 = require("../getDurationWithUnits"); const uiMode_1 = require("../uiMode"); /** * Get typed promise with his resolve and reject functions, * and with setted timeout. */ const getPromiseWithResolveAndReject = (timeoutInMs) => { let reject; let resolve; const promiseWithoutClear = new Promise((res, rej) => { resolve = res; reject = rej; }); (0, asserts_1.assertValueIsDefined)(reject, 'reject is defined', { promiseWithoutClear, resolve }); (0, asserts_1.assertValueIsDefined)(resolve, 'resolve is defined', { promiseWithoutClear, reject }); let rejectTimeoutFunction = () => { const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(timeoutInMs); const error = new error_1.E2edError(`Promise was rejected after ${timeoutWithUnits} timeout by default reject function`); reject?.(error); }; const timeoutId = setTimeout((async () => { try { await rejectTimeoutFunction(); } catch (error) { (0, fn_1.setCustomInspectOnFunction)(rejectTimeoutFunction); (0, generalLog_1.generalLog)('Reject timeout function rejected with error', { error, rejectTimeoutFunction }); } }), internal_1.IS_DEBUG || uiMode_1.isUiMode ? internal_1.MAX_TIMEOUT_IN_MS : timeoutInMs); const clearRejectTimeout = () => { clearTimeout(timeoutId); }; const setRejectTimeoutFunction = (newRejectTimeoutFunction) => { rejectTimeoutFunction = newRejectTimeoutFunction; }; const promiseWithTimeout = promiseWithoutClear.finally(clearRejectTimeout); return { clearRejectTimeout, promiseWithTimeout, reject, resolve, setRejectTimeoutFunction }; }; exports.getPromiseWithResolveAndReject = getPromiseWithResolveAndReject;