Skip to content

Commit

Permalink
FI-1344 fix: do not enable "full mocks" in skipped tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uid11 committed Aug 19, 2024
1 parent b19174b commit f660550
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 49 deletions.
2 changes: 0 additions & 2 deletions autotests/configurator/mapLogPayloadInConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export const mapLogPayloadInConsole: MapLogPayloadInConsole = (message, payload)
}

if (
message.startsWith('✓') ||
message.startsWith('✘') ||
message.startsWith('Caught an error when running tests in retry') ||
message.startsWith('Warning from TestCafe:') ||
message.startsWith('Usage:')
Expand Down
4 changes: 2 additions & 2 deletions src/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {CREATE_PAGE_TOKEN} from './constants/internal';
import {assertValueIsTrue} from './utils/asserts';
import {getFullPackConfig} from './utils/config';
import {reloadDocument} from './utils/document';
import {getPage} from './useContext';
import {getPlaywrightPage} from './useContext';

import type {PageRoute} from './PageRoute';
import type {AsyncVoid, PageClassTypeArgs, ParamsKeyType, Url} from './types/internal';
Expand Down Expand Up @@ -129,7 +129,7 @@ export abstract class Page<PageParams = undefined> {
* Waits for `DOMContentLoaded` event.
*/
async waitForDomContentLoaded(): Promise<void> {
const playwrightPage = getPage();
const playwrightPage = getPlaywrightPage();

await playwrightPage.waitForLoadState('domcontentloaded');
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/getCookies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LogEventType} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Cookie} from '../types/internal';
Expand All @@ -11,7 +11,7 @@ import type {Cookie} from '../types/internal';
export const getCookies = async (
cookiesParameters: Partial<Cookie> = {},
): Promise<readonly Cookie[]> => {
const page = getPage();
const page = getPlaywrightPage();
const parameters = Object.keys(cookiesParameters);

const allCookies = await page.context().cookies(page.url());
Expand Down
4 changes: 2 additions & 2 deletions src/actions/mock/mockApiRoute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LogEventType} from '../../constants/internal';
import {getApiMockState} from '../../context/apiMockState';
import {getFullMocksState} from '../../context/fullMocks';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';
import {assertValueIsDefined} from '../../utils/asserts';
import {setCustomInspectOnFunction} from '../../utils/fn';
import {log} from '../../utils/log';
Expand Down Expand Up @@ -64,7 +64,7 @@ export const mockApiRoute = async <
routeName: Route.name,
});

const page = getPage();
const page = getPlaywrightPage();

const setResponse = getSetResponse(apiMockState);

Expand Down
4 changes: 2 additions & 2 deletions src/actions/mock/unmockApiRoute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {LogEventType} from '../../constants/internal';
import {getApiMockState} from '../../context/apiMockState';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';
import {assertValueIsDefined} from '../../utils/asserts';
import {setCustomInspectOnFunction} from '../../utils/fn';
import {log} from '../../utils/log';
Expand Down Expand Up @@ -44,7 +44,7 @@ export const unmockApiRoute = async <
routeWasMocked,
});

const page = getPage();
const page = getPlaywrightPage();

await page.unroute(requestsFilter);
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/navigateToUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LogEventType} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Url} from '../types/internal';
Expand All @@ -18,7 +18,7 @@ export const navigateToUrl = async (url: Url, options: Options = {}): Promise<vo
log(`Will navigate to the url ${url}`, LogEventType.InternalAction);
}

const page = getPage();
const page = getPlaywrightPage();

await page.goto(url);

Expand Down
4 changes: 2 additions & 2 deletions src/actions/pressKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LogEventType} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Keyboard} from '@playwright/test';
Expand All @@ -14,7 +14,7 @@ type Options = Parameters<Keyboard['press']>[1];
export const pressKey = async (key: KeyboardPressKey, options: Options = {}): Promise<void> => {
log(`Press keyboard key: "${key}"`, options, LogEventType.InternalAction);

const page = getPage();
const page = getPlaywrightPage();

await page.keyboard.press(key, options);
};
4 changes: 2 additions & 2 deletions src/actions/setCookies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LogEventType} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Cookie} from '../types/internal';
Expand All @@ -10,7 +10,7 @@ import type {Cookie} from '../types/internal';
export const setCookies = async (cookies: readonly Cookie[]): Promise<void> => {
log('Set cookies with the specified cookies parameters', {cookies}, LogEventType.InternalAction);

const page = getPage();
const page = getPlaywrightPage();

const browserContext = page.context();

Expand Down
4 changes: 2 additions & 2 deletions src/actions/setHeadersAndNavigateToUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AsyncLocalStorage} from 'node:async_hooks';

import {LogEventType} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';
import {applyHeadersMapper} from '../utils/requestHooks';

Expand All @@ -15,7 +15,7 @@ import type {MapOptions, Url} from '../types/internal';
export const setHeadersAndNavigateToUrl = async (url: Url, options: MapOptions): Promise<void> => {
const {mapRequestHeaders, mapResponseHeaders} = options;

const page = getPage();
const page = getPlaywrightPage();

await page.route(
url,
Expand Down
4 changes: 2 additions & 2 deletions src/actions/takeScreenshot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join} from 'node:path';

import {LogEventType, SCREENSHOTS_DIRECTORY_PATH} from '../constants/internal';
import {getPage} from '../useContext';
import {getPlaywrightPage} from '../useContext';
import {log} from '../utils/log';

import type {Page} from '@playwright/test';
Expand All @@ -25,7 +25,7 @@ export const takeScreenshot = async (options: Options = {}): Promise<void> => {
options.path = join(SCREENSHOTS_DIRECTORY_PATH, pathToScreenshot);
}

const page = getPage();
const page = getPlaywrightPage();

await page.screenshot(options);
};
4 changes: 2 additions & 2 deletions src/actions/waitFor/waitForRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LogEventType} from '../../constants/internal';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';
import {getFullPackConfig} from '../../utils/config';
import {E2edError} from '../../utils/error';
import {setCustomInspectOnFunction} from '../../utils/fn';
Expand All @@ -22,7 +22,7 @@ export const waitForRequest = <SomeRequest extends Request>(
const {waitForRequestTimeout} = getFullPackConfig();
const rejectTimeout = timeout ?? waitForRequestTimeout;

const page = getPage();
const page = getPlaywrightPage();

const promise = page
.waitForRequest(
Expand Down
4 changes: 2 additions & 2 deletions src/actions/waitFor/waitForResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AsyncLocalStorage} from 'node:async_hooks';

import {LogEventType} from '../../constants/internal';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';
import {getFullPackConfig} from '../../utils/config';
import {setCustomInspectOnFunction} from '../../utils/fn';
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const waitForResponse = <
const {waitForResponseTimeout} = getFullPackConfig();
const rejectTimeout = timeout ?? waitForResponseTimeout;

const page = getPage();
const page = getPlaywrightPage();

const promise = page
.waitForResponse(
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ const playwrightConfig = defineConfig({
projects: [
{
name: 'chromium',
use: {...devices['Desktop Chrome']},
use: {
...devices['Desktop Chrome'],
viewport: {height: userlandPack.viewportHeight, width: userlandPack.viewportWidth},
},
},
],

Expand Down
4 changes: 2 additions & 2 deletions src/createClientFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {generalLog} from './utils/generalLog';
import {getDurationWithUnits} from './utils/getDurationWithUnits';
import {addTimeoutToPromise} from './utils/promise';
import {createTestRunCallback} from './utils/testRun';
import {getPage} from './useContext';
import {getPlaywrightPage} from './useContext';

import type {ClientFunction} from './types/internal';

Expand All @@ -24,7 +24,7 @@ export const createClientFunction = <Args extends readonly unknown[], Result>(
const printedClientFunctionName = `client function${name ? ` "${name}"` : ''}`;

const clientFunctionWithTimeout = (...args: Args): Promise<Result> => {
const page = getPage();
const page = getPlaywrightPage();

const clientFunctionTimeout = timeout ?? getTestIdleTimeout();

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export {ApiRoute} from './ApiRoute';
export {Page} from './Page';
export {PageRoute} from './PageRoute';
export {Route} from './Route';
export {useContext} from './useContext';
export {getPlaywrightPage, useContext} from './useContext';

/**
* Public modules, dependent on internal utils.
Expand Down
3 changes: 2 additions & 1 deletion src/useContext/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {getPlaywrightPage} from './page';
/** @internal */
export {getPage, pageStorage} from './page';
export {pageStorage} from './page';
export {useContext} from './useContext';
3 changes: 1 addition & 2 deletions src/useContext/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export const pageStorage = new AsyncLocalStorage<Page>();

/**
* Get `page` object from context of current test.
* @internal
*/
export const getPage = (): Page => {
export const getPlaywrightPage = (): Page => {
const maybePage = pageStorage.getStore();

assertValueIsDefined(maybePage, 'maybePage is defined');
Expand Down
8 changes: 4 additions & 4 deletions src/useContext/useContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getPage} from './page';
import {getPlaywrightPage} from './page';

import type {Page} from '@playwright/test';

Expand Down Expand Up @@ -28,7 +28,7 @@ export const useContext = (<Type>(defaultValue?: Type) => {
* Set value to test context.
*/
const set = (value: Type): void => {
const page: PageWithCtx = getPage();
const page: PageWithCtx = getPlaywrightPage();

if (page.ctx === undefined) {
page.ctx = Object.create(null) as {};
Expand All @@ -53,7 +53,7 @@ export const useContext = (<Type>(defaultValue?: Type) => {
* Get value from test context.
*/
const get = (): Type | undefined => {
const page: PageWithCtx = getPage();
const page: PageWithCtx = getPlaywrightPage();

if (page.ctx === undefined) {
page.ctx = Object.create(null) as {};
Expand All @@ -71,7 +71,7 @@ export const useContext = (<Type>(defaultValue?: Type) => {
* Get value from test context (or default value, if it is `undefined`).
*/
const getWithDefaultValue = (): Type => {
const page: PageWithCtx = getPage();
const page: PageWithCtx = getPlaywrightPage();

if (page.ctx === undefined) {
page.ctx = Object.create(null) as {};
Expand Down
4 changes: 2 additions & 2 deletions src/utils/events/registerEndTestRunEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isDebug, TestRunStatus} from '../../constants/internal';
import {getFullMocksState} from '../../context/fullMocks';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';

import {cloneWithoutLogEvents} from '../clone';
import {getRunErrorFromError} from '../error';
Expand Down Expand Up @@ -98,6 +98,6 @@ export const registerEndTestRunEvent = async (endTestRunEvent: EndTestRunEvent):
await writeLogsToFile();

if (isDebug) {
await getPage().pause();
await getPlaywrightPage().pause();
}
};
10 changes: 6 additions & 4 deletions src/utils/selectors/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {RETRY_KEY} from '../../constants/internal';
import {getFrameContext} from '../../context/frameContext';
import {getPage} from '../../useContext';
import {getPlaywrightPage} from '../../useContext';

import type {Locator as PlaywrightLocator} from '@playwright/test';

Expand Down Expand Up @@ -177,13 +177,15 @@ export class Selector {

switch (this.kind) {
case 'css':
return (getFrameContext() ?? getPage()).locator(this.cssString);
return (getFrameContext() ?? getPlaywrightPage()).locator(this.cssString);

case 'filter':
return selector.getPlaywrightLocator().and(getPage().locator(String(args[0])));
return selector.getPlaywrightLocator().and(getPlaywrightPage().locator(String(args[0])));

case 'find':
return selector.getPlaywrightLocator().locator(getPage().locator(String(args[0])));
return selector
.getPlaywrightLocator()
.locator(getPlaywrightPage().locator(String(args[0])));

case 'nth':
return selector.getPlaywrightLocator().nth(Number(args[0]));
Expand Down
12 changes: 4 additions & 8 deletions src/utils/test/runTestFn.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {TestRunStatus} from '../../constants/internal';
import {setTestRunPromise} from '../../context/testRunPromise';
import {getTestTimeout} from '../../context/testTimeout';

Expand Down Expand Up @@ -29,7 +30,7 @@ export const runTestFn = async ({
testController,
testStaticOptions,
}: Options): Promise<void> => {
const testRunEvent = getTestRunEvent(runId);
const {status, testFnWithReject} = getTestRunEvent(runId);
const testTimeout = getTestTimeout();

const {promiseWithTimeout: testRunPromise, resolve: resolveTestRunPromise} =
Expand All @@ -39,18 +40,13 @@ export const runTestFn = async ({

setTestRunPromise(testRunPromise);

// TODO: support waitForEventsState
// const waitForEventsState = getWaitForEventsState();

const {fullMocks} = getFullPackConfig();

if (fullMocks?.filterTests(testStaticOptions)) {
if (status !== TestRunStatus.Skipped && fullMocks?.filterTests(testStaticOptions)) {
const shouldApplyMocks = retry === 1;

await enableFullMocks(fullMocks, shouldApplyMocks, testStaticOptions.filePath);
}

await testRunEvent
.testFnWithReject(testController)
.finally(() => resolveTestRunPromise(undefined));
await testFnWithReject(testController).finally(() => resolveTestRunPromise(undefined));
};

0 comments on commit f660550

Please sign in to comment.