UNPKG

@ngx-playwright/jest

Version:
131 lines 4.09 kB
"use strict"; // eslint-disable-next-line @typescript-eslint/triple-slash-reference /// <reference path="../global.d.ts" /> Object.defineProperty(exports, "__esModule", { value: true }); const testing_1 = require("@angular/cdk/testing"); const harness_1 = require("@ngx-playwright/harness"); const parse_arguments_1 = require("../utils/parse-arguments"); const cachedEnvironments = new WeakMap(); function getOrCreateEnvironment(page) { let environment = cachedEnvironments.get(page); if (environment == null) { environment = harness_1.createEnvironment(page); cachedEnvironments.set(page, environment); } return environment; } function isPromiseLike(value) { return !!value && typeof value.then === 'function'; } Object.defineProperty(globalThis, 'harnessEnvironment', { configurable: true, get: () => getOrCreateEnvironment(page), }); // eslint-disable-next-line @typescript-eslint/no-explicit-any global.autoStabilize = (fn) => { if (!fn) { harness_1.autoStabilize(() => page); return; } return () => { if (harness_1.isAutoStabilizing()) { // already automatically stabilizing return fn(); } harness_1.autoStabilize(() => page); let isPromise = false; try { const result = fn(); if (isPromiseLike(result)) { isPromise = true; return Promise.resolve(result).finally(() => manuallyStabilize()); } else { return result; } } finally { if (!isPromise) { manuallyStabilize(); } } }; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any global.manuallyStabilize = (fn) => { if (!fn) { harness_1.manuallyStabilize(); return; } return () => { if (!harness_1.isAutoStabilizing()) { // already manually stabilizing return fn(); } harness_1.manuallyStabilize(); let isPromise = false; try { const result = fn(); if (isPromiseLike(result)) { isPromise = true; return Promise.resolve(result).finally(() => autoStabilize()); } else { return result; } } finally { if (!isPromise) { autoStabilize(); } } }; }; function inScreen(pageOrScreen, screenOrFn, fn) { let page; let Screen; let testFunction; if (typeof pageOrScreen === 'function') { // @ts-expect-error Somehow global.page can't be found page = global.page; Screen = pageOrScreen; testFunction = screenOrFn; } else { page = pageOrScreen; Screen = screenOrFn; testFunction = fn; } const args = parse_arguments_1.getDestructuredArguments(testFunction); return async function () { await openScreen(Screen, page, baseUrl); const screen = await getOrCreateEnvironment(page).getHarness(Screen); if (args == null) { await testFunction({}, screen); } else { const properties = await testing_1.parallel(() => args.map(async (name) => { var _a; // @ts-expect-error keyof T _can_ in fact index ExtractablePropertiesOfScreen<T> const value = await ((_a = screen[name]) === null || _a === void 0 ? void 0 : _a.call(screen)); return [name, value]; })); await testFunction(Object.fromEntries(properties), screen); } }; } function hasOpenFunction(screen) { return (typeof screen .open === 'function'); } function openScreen(screen, page, baseUrl) { if (hasOpenFunction(screen)) { return screen.open(page, baseUrl); } else { return page.goto(new URL(screen.path, baseUrl).href); } } global.inScreen = inScreen; autoStabilize(); //# sourceMappingURL=beforeAll.js.map