Skip to content

Commit

Permalink
Merge pull request #98 from joomcode/fix/add-logs-to-rerequests
Browse files Browse the repository at this point in the history
feat: add `addLogsWithTags` field to pack config
  • Loading branch information
uid11 authored Dec 4, 2024
2 parents e9e966c + 41ad5f7 commit 692f380
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ that exports the pack's config under the name `pack`.

Here are the basic fields of the pack config.

`addLogsWithTags: readonly LogTag[]`: array of additional log tags. Logs with a specific tag
(in `logTag` field) will be added only if their tag is specified in this array.

`browserFlags: string[]`: array of browser flags, like `--disable-dev-shm-usage`,
with which the browser is launched to run tests.

Expand Down
1 change: 1 addition & 0 deletions autotests/packs/allTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const waitBeforeRetryTimeout = 1_000;
* Pack of tests or tasks (pack configuration object).
*/
export const pack: Pack = {
addLogsWithTags: [],
assertionTimeout: 5_000,
browserFlags,
browserName: 'chromium',
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"eslint-plugin-simple-import-sort": "12.1.1",
"eslint-plugin-typescript-sort-keys": "3.3.0",
"husky": "9.1.7",
"prettier": "3.4.1",
"prettier": "3.4.2",
"typescript": "5.7.2"
},
"peerDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/types/config/ownE2edConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {PlaywrightTestConfig} from '@playwright/test';
import type {TestRunStatus} from '../../constants/internal';

import type {FullMocksConfig} from '../fullMocks';
import type {MapBackendResponseToLog, MapLogPayload, MapLogPayloadInReport} from '../log';
import type {LogTag, MapBackendResponseToLog, MapLogPayload, MapLogPayloadInReport} from '../log';
import type {MaybePromise} from '../promise';
import type {LiteReport} from '../report';
import type {TestOptions, TestStaticOptions} from '../testRun';
Expand All @@ -26,6 +26,12 @@ export type OwnE2edConfig<
SkipTests = SkipTestsPlaceholder,
TestMeta = TestMetaPlaceholder,
> = Readonly<{
/**
* Array of additional log tags. Logs with a specific tag (in `logTag` field)
* will be added only if their tag is specified in this array.
*/
addLogsWithTags: readonly LogTag[];

/**
* Array of browser flags, like `--disable-dev-shm-usage`, with which the browser is launched to run tests.
*/
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type {
Url,
} from './http';
export type {KeyboardPressKey} from './keyboard';
export type {Log, LogContext, LogParams, LogPayload, LogTag} from './log';
export type {ApiMockFunction} from './mockApiRoute';
export type {WebSocketMockFunction} from './mockWebSocketRoute';
export type {NavigateToUrlOptions} from './navigation';
Expand Down
1 change: 1 addition & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type {
LogContext,
LogParams,
LogPayload,
LogTag,
MapBackendResponseToLog,
MapLogPayload,
MapLogPayloadInReport,
Expand Down
6 changes: 6 additions & 0 deletions src/types/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ export type LogPayload = Readonly<{
backendResponses?: readonly Payload[];
filePath?: unknown;
logEventStatus?: LogEventStatus;
logTag?: LogTag;
successful?: unknown;
}> &
Payload;

/**
* Type for `log` tags.
*/
export type LogTag = 'waitForAllRequestsComplete';

/**
* Maps responses from the backend to logs during the test.
* Log the `responseBody` field carefully, as the body of backend response can be very large.
Expand Down
7 changes: 6 additions & 1 deletion src/utils/log/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export const log: Log = (message, maybePayload?: unknown, maybeLogEventType?: un
? (maybePayload as LogEventType)
: ((maybeLogEventType as LogEventType) ?? LogEventType.Unspecified);

const {mapLogPayloadInReport} = getFullPackConfig();
const {addLogsWithTags, mapLogPayloadInReport} = getFullPackConfig();

if (payload && 'logTag' in payload && !addLogsWithTags.includes(payload.logTag)) {
return;
}

const payloadInReport = mapLogPayloadInReport(message, payload, type);

registerLogEvent(runId, {message, payload: payloadInReport, time, type});
Expand Down
8 changes: 1 addition & 7 deletions src/utils/test/preparePage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AsyncLocalStorage} from 'node:async_hooks';

import {getConsoleMessagesFromContext} from '../../context/consoleMessages';
import {getIsPageNavigatingNow, setIsPageNavigatingNow} from '../../context/isPageNavigatingNow';
import {setIsPageNavigatingNow} from '../../context/isPageNavigatingNow';
import {getJsErrorsFromContext} from '../../context/jsError';
import {getNavigationDelay} from '../../context/navigationDelay';
import {getOnResponseCallbacks} from '../../context/onResponseCallbacks';
Expand Down Expand Up @@ -49,7 +49,6 @@ export const preparePage = async (page: Page): Promise<() => Promise<void>> => {
);

let navigationRequestsCount = 0;
const skippedRequestUrls = Object.create(null) as Record<string, true>;

const consoleListener = AsyncLocalStorage.bind(async (message: PlaywrightConsoleMessage) => {
const args: unknown[] = [];
Expand All @@ -73,11 +72,6 @@ export const preparePage = async (page: Page): Promise<() => Promise<void>> => {

const requestListener = AsyncLocalStorage.bind(async (newRequest: PlaywrightRequest) => {
const isNavigationRequest = newRequest.isNavigationRequest();
const isPageNavigatingNow = getIsPageNavigatingNow();

if (isPageNavigatingNow) {
skippedRequestUrls[newRequest.url()] = true;
}

if (isNavigationRequest) {
navigationRequestsCount += 1;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/waitForEvents/addNotCompleteRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {assertValueIsDefined} from '../asserts';
import {log} from '../log';

import {isReRequest} from './isReRequest';
import {processAllRequestsCompletePredicates} from './processAllRequestsCompletePredicates';
Expand All @@ -22,6 +23,11 @@ export const addNotCompleteRequest = async (

hashOfNotCompleteRequests[requestHookContextId] = request;

log(`Add not complete request with url ${request.url}`, {
logTag: 'waitForAllRequestsComplete',
requestHookContextId,
});

for (const previousRequestId of Object.keys(
hashOfNotCompleteRequests,
) as RequestHookContextId[]) {
Expand All @@ -37,6 +43,11 @@ export const addNotCompleteRequest = async (
});

if (isReRequest(request, previousRequest)) {
log(`Remove duplicate request with url ${previousRequest.url}`, {
logTag: 'waitForAllRequestsComplete',
previousRequestId,
});

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete hashOfNotCompleteRequests[previousRequestId];
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/waitForEvents/completeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {log} from '../log';

import type {RequestHookContextId, WaitForEventsState} from '../../types/internal';

/**
Expand All @@ -10,6 +12,11 @@ export const completeRequest = (
): void => {
const {allRequestsCompletePredicates, hashOfNotCompleteRequests} = waitForEventsState;

log(`Complete request with url ${hashOfNotCompleteRequests[requestHookContextId]?.url}`, {
logTag: 'waitForAllRequestsComplete',
requestHookContextId,
});

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete hashOfNotCompleteRequests[requestHookContextId];

Expand Down

0 comments on commit 692f380

Please sign in to comment.