UNPKG

e2ed

Version:

E2E testing framework over Playwright

74 lines (73 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTestFnAndReject = void 0; const internal_1 = require("../../constants/internal"); const error_1 = require("../error"); const fs_1 = require("../fs"); const generalLog_1 = require("../generalLog"); const getDurationWithUnits_1 = require("../getDurationWithUnits"); const promise_1 = require("../promise"); const skippedTestFnAndReject = { onlog: () => undefined, reject: () => undefined, testFnWithReject: () => internal_1.RESOLVED_PROMISE, }; /** * Get test function with execution timeout, idle timeout, reject and onlog functions, * by isSkipped flag, test function, runId, test execution timeout and test idle timeouts. * @internal */ const getTestFnAndReject = ({ isSkipped, runId, testFn, testIdleTimeout, testTimeout, }) => { if (isSkipped) { return skippedTestFnAndReject; } const { clearRejectTimeout, promiseWithTimeout, reject: rejectPromise, setRejectTimeoutFunction, } = (0, promise_1.getPromiseWithResolveAndReject)(testTimeout); let isTestRunCompleted = false; let idleTimeoutId; const testFnWithReject = (testController) => Promise.race([testFn(testController), promiseWithTimeout]).finally(() => { isTestRunCompleted = true; clearTimeout(idleTimeoutId); clearRejectTimeout(); }); /** * Resets idle timeout on log call. * @internal */ const onlog = () => { clearTimeout(idleTimeoutId); void (0, fs_1.writeLogEventTime)(); if (isTestRunCompleted) { return; } // eslint-disable-next-line @typescript-eslint/no-use-before-define idleTimeoutId = setTimeout(rejectByIdleTimeoutError, testIdleTimeout); }; /** * Rejects test run by some run error. * @internal */ const reject = (error) => { if (isTestRunCompleted) { // TODO: save errors that come after the test run is completed. return; } (0, generalLog_1.generalLog)(`Reject test run ${runId} with run error`, { error }); rejectPromise(error); }; /** * Rejects test run by test idle timeout error (timeout between steps). * @internal */ function rejectByIdleTimeoutError() { const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(testIdleTimeout); const error = new error_1.E2edError(`Test run ${runId} was rejected after ${timeoutWithUnits} idle timeout`); reject(error); } setRejectTimeoutFunction(() => { const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(testTimeout); const error = new error_1.E2edError(`Test run ${runId} was rejected after ${timeoutWithUnits} timeout`); reject(error); }); return { onlog, reject, testFnWithReject }; }; exports.getTestFnAndReject = getTestFnAndReject;