e2ed
Version:
E2E testing framework over Playwright
41 lines (40 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitForResponse = void 0;
const node_async_hooks_1 = require("node:async_hooks");
const useContext_1 = require("../../useContext");
const config_1 = require("../../utils/config");
const fn_1 = require("../../utils/fn");
const getDurationWithUnits_1 = require("../../utils/getDurationWithUnits");
const log_1 = require("../../utils/log");
const requestHooks_1 = require("../../utils/requestHooks");
const waitForEvents_1 = require("../../utils/waitForEvents");
/**
* Waits for some response (from browser) filtered by the response predicate.
* If the function runs longer than the specified timeout, it is rejected.
*/
exports.waitForResponse = (async (predicate, triggerOrOptions, options) => {
const startTimeInMs = Date.now();
(0, fn_1.setCustomInspectOnFunction)(predicate);
const trigger = typeof triggerOrOptions === 'function' ? triggerOrOptions : undefined;
const finalOptions = typeof triggerOrOptions === 'function' ? options : triggerOrOptions;
const timeout = finalOptions?.timeout ?? (0, config_1.getFullPackConfig)().waitForResponseTimeout;
if (trigger !== undefined) {
(0, fn_1.setCustomInspectOnFunction)(trigger);
}
const page = (0, useContext_1.getPlaywrightPage)();
const promise = page
.waitForResponse(node_async_hooks_1.AsyncLocalStorage.bind((0, waitForEvents_1.getWaitForResponsePredicate)(predicate, finalOptions?.includeNavigationRequest ?? false, timeout)), { timeout })
.then((playwrightResponse) => (0, requestHooks_1.getResponseFromPlaywrightResponse)(playwrightResponse));
const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(timeout);
if (finalOptions?.skipLogs !== true) {
(0, log_1.log)(`Set wait for response with timeout ${timeoutWithUnits}`, { predicate, trigger }, 7 /* LogEventType.InternalCore */);
}
await trigger?.();
const response = await promise;
if (finalOptions?.skipLogs !== true) {
const waitWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(Date.now() - startTimeInMs);
(0, log_1.log)(`Have waited for response for ${waitWithUnits}`, { predicate, response, timeoutWithUnits, trigger }, 7 /* LogEventType.InternalCore */);
}
return response;
});