Skip to content

Commit 44a1fa8

Browse files
committed
E2E-275 fix: renew last log event time on start of each test
1 parent 08f3af5 commit 44a1fa8

9 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/actions/switchToMainTab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {switchPlaywrightPage} from '../utils/playwrightPage';
77
/**
88
* Switches page context to the specified tab.
99
*/
10-
export const switchToMainTab = (): void => {
10+
export const switchToMainTab = async (): Promise<void> => {
1111
clearTab();
1212

1313
const page = getPlaywrightPage();
1414
const url = page.url();
1515

1616
log(`Switch page context to the main tab at ${url}`, LogEventType.InternalAction);
1717

18-
switchPlaywrightPage(page);
18+
await switchPlaywrightPage(page);
1919
};

src/actions/switchToTab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import type {InternalTab, Tab} from '../types/internal';
88
/**
99
* Switches page context to the specified tab.
1010
*/
11-
export const switchToTab = (tab: Tab): void => {
11+
export const switchToTab = async (tab: Tab): Promise<void> => {
1212
const {page} = tab as InternalTab;
1313
const url = page.url();
1414

1515
log(`Switch page context to the specified tab at ${url}`, LogEventType.InternalAction);
1616

1717
setTab(tab);
1818

19-
switchPlaywrightPage(page);
19+
await switchPlaywrightPage(page);
2020
};

src/context/clearPage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {useContext} from '../useContext';
2+
3+
/**
4+
* Get and set internal (maybe `undefined`) `clearPage` function.
5+
* @internal
6+
*/
7+
export const [getClearPage, setClearPage] = useContext<() => Promise<void>>();

src/utils/playwrightPage/switchPlaywrightPage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {getClearPage} from '../../context/clearPage';
2+
3+
import {preparePage} from '../test';
4+
15
import {pageWaitForRequest, waitForRequestCalls} from './waitForRequest';
26
import {pageWaitForResponse, waitForResponseCalls} from './waitForResponse';
37

@@ -8,7 +12,13 @@ import type {Page} from '@playwright/test';
812
* Removes all event handlers from the old page and moves them to the new one.
913
* @internal
1014
*/
11-
export const switchPlaywrightPage = (newPage: Page): void => {
15+
export const switchPlaywrightPage = async (newPage: Page): Promise<void> => {
16+
const clearPage = getClearPage();
17+
18+
await clearPage?.();
19+
20+
await preparePage(newPage);
21+
1222
const oldRequestCalls = [...waitForRequestCalls];
1323

1424
waitForRequestCalls.length = 0;

src/utils/test/afterTest.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import {getClearPage} from '../../context/clearPage';
2+
13
import {registerEndTestRunEvent} from '../events';
24
import {generalLog, writeLogsToFile} from '../generalLog';
35

46
import type {EndTestRunEvent, UtcTimeInMs} from '../../types/internal';
57

6-
type Options = Readonly<{clearPage: (() => Promise<void>) | undefined}> &
7-
Omit<EndTestRunEvent, 'utcTimeInMs'>;
8+
type Options = Omit<EndTestRunEvent, 'utcTimeInMs'>;
89

910
/**
1011
* Internal after test hook.
1112
* @internal
1213
*/
1314
export const afterTest = async (options: Options): Promise<void> => {
14-
const {clearPage, ...optionsWithoutClearPage} = options;
1515
const utcTimeInMs = Date.now() as UtcTimeInMs;
16-
const endTestRunEvent: EndTestRunEvent = {...optionsWithoutClearPage, utcTimeInMs};
16+
const endTestRunEvent: EndTestRunEvent = {...options, utcTimeInMs};
1717

1818
try {
19+
const clearPage = getClearPage();
20+
1921
await clearPage?.();
2022

2123
await registerEndTestRunEvent(endTestRunEvent);

src/utils/test/beforeTest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {addResponseToApiStatistics} from '../apiStatistics';
1212
import {getFullPackConfig} from '../config';
1313
import {getRunLabel} from '../environment';
1414
import {registerStartTestRunEvent} from '../events';
15+
import {writeLogEventTime} from '../fs';
1516
import {mapBackendResponseForLogs} from '../log';
1617
import {isUiMode} from '../uiMode';
1718
import {getUserlandHooks} from '../userland';
@@ -97,4 +98,6 @@ export const beforeTest = ({
9798
};
9899

99100
registerStartTestRunEvent(testRunEvent);
101+
102+
void writeLogEventTime().catch(() => {});
100103
};

src/utils/test/getRunTest.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const getRunTest =
2727
const retryIndex = testInfo.retry + 1;
2828
const runId = createRunId(test, retryIndex);
2929

30-
let clearPage: (() => Promise<void>) | undefined;
3130
let hasRunError = false;
3231
let shouldRunTest = false;
3332
let testStaticOptions: TestStaticOptions | undefined;
@@ -52,7 +51,7 @@ export const getRunTest =
5251
testStaticOptions,
5352
};
5453

55-
clearPage = await preparePage(page);
54+
await preparePage(page);
5655

5756
beforeTest(testUnit);
5857

@@ -68,7 +67,7 @@ export const getRunTest =
6867
throw error;
6968
} finally {
7069
if (shouldRunTest) {
71-
await afterTest({clearPage, hasRunError, runId, unknownRunError});
70+
await afterTest({hasRunError, runId, unknownRunError});
7271
}
7372
}
7473
};

src/utils/test/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/** @internal */
22
export {getRunTest} from './getRunTest';
3+
/** @internal */
4+
export {preparePage} from './preparePage';

src/utils/test/preparePage.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {AsyncLocalStorage} from 'node:async_hooks';
22

3+
import {setClearPage} from '../../context/clearPage';
34
import {getConsoleMessagesFromContext} from '../../context/consoleMessages';
45
import {setIsPageNavigatingNow} from '../../context/isPageNavigatingNow';
56
import {getJsErrorsFromContext} from '../../context/jsError';
@@ -29,7 +30,7 @@ const afterNavigationRequestsDelayInMs = 300;
2930
* Prepares page before test.
3031
* @internal
3132
*/
32-
export const preparePage = async (page: Page): Promise<() => Promise<void>> => {
33+
export const preparePage = async (page: Page): Promise<void> => {
3334
const consoleMessages = getConsoleMessagesFromContext() as ConsoleMessage[];
3435
const jsErrors = getJsErrorsFromContext() as JsError[];
3536
const navigationDelay = getNavigationDelay();
@@ -133,7 +134,7 @@ export const preparePage = async (page: Page): Promise<() => Promise<void>> => {
133134
page.on('response', responseListener);
134135
page.on('requestfinished', requestfinishedListener);
135136

136-
return async () => {
137+
const clearPage = async (): Promise<void> => {
137138
page.removeListener('console', consoleListener);
138139
page.removeListener('pageerror', pageerrorListener);
139140
page.removeListener('request', requestListener);
@@ -142,4 +143,6 @@ export const preparePage = async (page: Page): Promise<() => Promise<void>> => {
142143

143144
await page.unrouteAll({behavior: 'ignoreErrors'}).catch(() => {});
144145
};
146+
147+
setClearPage(clearPage);
145148
};

0 commit comments

Comments
 (0)