Skip to content

Commit 5935f7d

Browse files
committed
fix: skip TestUnhandledRejection errors with TARGET_CLOSED_ERROR_MESSAGE
fix: don't catch timeout errors in `expect` matchers
1 parent 4980a0c commit 5935f7d

11 files changed

Lines changed: 42 additions & 26 deletions

File tree

autotests/tests/switchingPagesForResponses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ test(
6363

6464
await expect(
6565
numberOfSentRequests === numberOfCaughtResponses ||
66+
numberOfSentRequests === numberOfCaughtResponses + 2 ||
6667
numberOfSentRequests === numberOfCaughtResponses + 1 ||
6768
numberOfSentRequests === numberOfCaughtResponses - 1,
6869
`almost all responses were caught (${numberOfCaughtResponses} of ${numberOfSentRequests})`,

src/ApiRoute.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ export abstract class ApiRoute<
1313
/**
1414
* Request type of API route.
1515
*/
16-
// eslint-disable-next-line @typescript-eslint/naming-convention
17-
declare readonly __REQUEST_KEY: SomeRequest;
16+
declare readonly Request: SomeRequest;
1817

1918
/**
2019
* Response type of API route.
2120
*/
22-
// eslint-disable-next-line @typescript-eslint/naming-convention
23-
declare readonly __RESPONSE_KEY: SomeResponse;
21+
declare readonly Response: SomeResponse;
2422

2523
/**
2624
* Returns `true`, if the request body is in JSON format.

src/WebSocketRoute.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ export abstract class WebSocketRoute<
1616
/**
1717
* Request type of WebSocket route.
1818
*/
19-
// eslint-disable-next-line @typescript-eslint/naming-convention
20-
declare readonly __REQUEST_KEY: SomeRequest;
19+
declare readonly Request: SomeRequest;
2120

2221
/**
2322
* Response type of WebSocket route.
2423
*/
25-
// eslint-disable-next-line @typescript-eslint/naming-convention
26-
declare readonly __RESPONSE_KEY: SomeResponse;
24+
declare readonly Response: SomeResponse;
2725

2826
/**
2927
* Returns `true`, if the request body is in JSON format.

src/constants/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export {
6363
TMP_DIRECTORY_PATH,
6464
} from './paths';
6565
/** @internal */
66-
export {TEST_ENDED_ERROR_MESSAGE} from './playwright';
66+
export {TARGET_CLOSED_ERROR_MESSAGE, TEST_ENDED_ERROR_MESSAGE} from './playwright';
6767
/** @internal */
6868
export {RESOLVED_PROMISE} from './promise';
6969
/** @internal */

src/constants/playwright.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Playwright error message for already closed target (`TargetClosedError`).
3+
* @internal
4+
*/
5+
export const TARGET_CLOSED_ERROR_MESSAGE = 'Target page, context or browser has been closed';
6+
17
/**
28
* Playwright error message for already ended test.
39
* @internal

src/createClientFunction.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {TEST_ENDED_ERROR_MESSAGE} from './constants/internal';
1+
import {TARGET_CLOSED_ERROR_MESSAGE, TEST_ENDED_ERROR_MESSAGE} from './constants/internal';
22
import {getTestIdleTimeout} from './context/testIdleTimeout';
33
import {E2edError} from './utils/error';
44
import {setCustomInspectOnFunction} from './utils/fn';
@@ -13,7 +13,6 @@ import type {ClientFunction} from './types/internal';
1313
type Options = Readonly<{name?: string; retries?: number; timeout?: number}>;
1414

1515
const contextErrorMessage = 'Execution context was destroyed';
16-
const targetErrorMessage = 'Target page, context or browser has been closed';
1716

1817
/**
1918
* Creates a client function.
@@ -48,7 +47,7 @@ export const createClientFunction = <Args extends readonly unknown[], Result>(
4847

4948
if (
5049
errorString.includes(contextErrorMessage) ||
51-
errorString.includes(targetErrorMessage) ||
50+
errorString.includes(TARGET_CLOSED_ERROR_MESSAGE) ||
5251
errorString.includes(TEST_ENDED_ERROR_MESSAGE)
5352
) {
5453
await page.waitForLoadState();
@@ -58,7 +57,7 @@ export const createClientFunction = <Args extends readonly unknown[], Result>(
5857

5958
if (
6059
suberrorString.includes(contextErrorMessage) ||
61-
suberrorString.includes(targetErrorMessage) ||
60+
suberrorString.includes(TARGET_CLOSED_ERROR_MESSAGE) ||
6261
suberrorString.includes(TEST_ENDED_ERROR_MESSAGE)
6362
) {
6463
return new Promise(() => {});
@@ -80,7 +79,7 @@ export const createClientFunction = <Args extends readonly unknown[], Result>(
8079

8180
if (
8281
suberrorString.includes(contextErrorMessage) ||
83-
suberrorString.includes(targetErrorMessage) ||
82+
suberrorString.includes(TARGET_CLOSED_ERROR_MESSAGE) ||
8483
suberrorString.includes(TEST_ENDED_ERROR_MESSAGE)
8584
) {
8685
return new Promise(() => {});

src/utils/expect/createExpectMethod.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ export const createExpectMethod = (
9292
}
9393

9494
return runAssertion(this.actualValue);
95-
}).catch((error: Error) => ({
96-
actualValue: this.actualValue,
97-
description: this.description,
98-
error,
99-
}));
95+
});
10096

10197
return assertionPromise.then(({actualValue, additionalLogFields, error}) => {
10298
const logMessage = `Assert: ${this.description}`;

src/utils/getGlobalErrorHandler.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {TARGET_CLOSED_ERROR_MESSAGE, TEST_ENDED_ERROR_MESSAGE} from '../constants/internal';
2+
13
import {E2edError} from './error';
24
import {writeGlobalError} from './fs';
35
import {writeLogsToFile} from './generalLog';
@@ -11,8 +13,18 @@ import type {GlobalErrorType} from '../types/internal';
1113
export const getGlobalErrorHandler =
1214
(type: GlobalErrorType) =>
1315
(cause: unknown): void => {
14-
const error = new E2edError(`Caught ${type}`, {cause});
16+
try {
17+
const error = new E2edError(`Caught ${type}`, {cause});
18+
const errorString = error.toString();
19+
20+
if (
21+
errorString.includes(TARGET_CLOSED_ERROR_MESSAGE) ||
22+
errorString.includes(TEST_ENDED_ERROR_MESSAGE)
23+
) {
24+
return;
25+
}
1526

16-
void writeGlobalError(error.toString()).catch(() => {});
17-
void writeLogsToFile().catch(() => {});
27+
void writeGlobalError(errorString).catch(() => {});
28+
void writeLogsToFile().catch(() => {});
29+
} catch {}
1830
};

src/utils/report/client/chooseTestRun.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export function chooseTestRun(runHash: RunHash): void {
3838

3939
const {testRunDetailsElementsByHash} = reportClientState;
4040

41-
const previousTestRunDetailsElement = e2edRightColumnContainer.firstElementChild as HTMLElement;
41+
const previousTestRunDetailsElement =
42+
e2edRightColumnContainer.firstElementChild as HTMLElement | null;
43+
44+
if (!previousTestRunDetailsElement) {
45+
return;
46+
}
4247

4348
if (
4449
!(previousHash in testRunDetailsElementsByHash) &&

src/utils/report/client/render/renderApiStatistics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export function renderApiStatistics({apiStatistics, hash}: Options): SafeHtml {
3434
let pageDuration = 0;
3535
const pageItems: SafeHtml[] = [];
3636

37-
for (const [url, {count, duration}] of Object.entries(byUrl)) {
37+
for (const [url, {count, duration}] of Object.entries(byUrl) as ObjectEntries<typeof byUrl>) {
3838
pageCount += count;
3939
pageDuration += duration;
4040

41-
pageItems.push(renderApiStatisticsItem({count, duration, name: url}));
41+
pageItems.push(renderApiStatisticsItem({count, duration, name: url, url}));
4242
}
4343

4444
items.push(

0 commit comments

Comments
 (0)