UNPKG

e2ed

Version:

E2E testing framework over Playwright

75 lines (74 loc) 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createClientFunction = void 0; const internal_1 = require("./constants/internal"); const testIdleTimeout_1 = require("./context/testIdleTimeout"); const error_1 = require("./utils/error"); const fn_1 = require("./utils/fn"); const generalLog_1 = require("./utils/generalLog"); const getDurationWithUnits_1 = require("./utils/getDurationWithUnits"); const promise_1 = require("./utils/promise"); const testRun_1 = require("./utils/testRun"); const useContext_1 = require("./useContext"); const contextErrorMessage = 'Execution context was destroyed'; /** * Creates a client function. */ const createClientFunction = (originalFn, { name: nameFromOptions, retries = 0, timeout } = {}) => { (0, fn_1.setCustomInspectOnFunction)(originalFn); const name = nameFromOptions ?? originalFn.name; const printedClientFunctionName = `client function${name ? ` "${name}"` : ''}`; const clientFunctionWithTimeout = (...args) => { const page = (0, useContext_1.getPlaywrightPage)(); const clientFunctionTimeout = timeout ?? (0, testIdleTimeout_1.getTestIdleTimeout)(); const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(clientFunctionTimeout); const error = new error_1.E2edError(`Client function "${name}" rejected after ${timeoutWithUnits} timeout`); // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func const func = new Function('args', `return (${originalFn.toString()})(...args)`); return (0, promise_1.addTimeoutToPromise)(page.evaluate(func, args).catch(async (evaluateError) => { const errorString = String(evaluateError); if (errorString.includes(contextErrorMessage) || errorString.includes(internal_1.TARGET_CLOSED_ERROR_MESSAGE) || errorString.includes(internal_1.TEST_ENDED_ERROR_MESSAGE)) { await page.waitForLoadState(); return page.evaluate(func, args).catch((suberror) => { const suberrorString = String(suberror); if (suberrorString.includes(contextErrorMessage) || suberrorString.includes(internal_1.TARGET_CLOSED_ERROR_MESSAGE) || suberrorString.includes(internal_1.TEST_ENDED_ERROR_MESSAGE)) { return new Promise(() => { }); } throw suberror; }); } if (retries > 0) { let retryIndex = 1; while (retryIndex <= retries) { retryIndex += 1; try { return page.evaluate(func, args).catch((suberror) => { const suberrorString = String(suberror); if (suberrorString.includes(contextErrorMessage) || suberrorString.includes(internal_1.TARGET_CLOSED_ERROR_MESSAGE) || suberrorString.includes(internal_1.TEST_ENDED_ERROR_MESSAGE)) { return new Promise(() => { }); } throw suberror; }); } catch { } } } throw evaluateError; }), clientFunctionTimeout, error); }; (0, generalLog_1.generalLog)(`Create ${printedClientFunctionName}`, { originalFn }); return (...args) => { const clientFunctionWithTestRun = (0, testRun_1.createTestRunCallback)({ targetFunction: clientFunctionWithTimeout, throwExceptionAtCallPoint: true, }); return clientFunctionWithTestRun(...args); }; }; exports.createClientFunction = createClientFunction;