Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/constants/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ const averageLineLength = 100;
*/
export const DEFAULT_INSPECT_OPTIONS = {
colors: false,
depth: 16,
numericSeparator: true,
depth: 12,
getters: false,
numericSeparator: false,
showHidden: true,
} as const;

/**
* Default max number of lines in printed stringify values (in logs).
* @internal
*/
export const DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE = 300;

/**
* Inspect options for output to console.
* @internal
Expand All @@ -26,15 +33,9 @@ export const CONSOLE_INSPECT_OPTIONS = {
*/
export const MAX_ELEMENTS_COUNT_IN_PRINTED_ARRAY = 8;

/**
* Max number of lines in printed stringify values (in logs).
* @internal
*/
export const MAX_LINES_COUNT_IN_PRINTED_VALUE = 300;

/**
* Max string length in printed stringify values (in logs).
* @internal
*/
export const MAX_STRING_LENGTH_IN_PRINTED_VALUE =
averageLineLength * MAX_LINES_COUNT_IN_PRINTED_VALUE;
averageLineLength * DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE;
2 changes: 1 addition & 1 deletion src/constants/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export {
export {
CONSOLE_INSPECT_OPTIONS,
DEFAULT_INSPECT_OPTIONS,
DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
MAX_ELEMENTS_COUNT_IN_PRINTED_ARRAY,
MAX_LINES_COUNT_IN_PRINTED_VALUE,
MAX_STRING_LENGTH_IN_PRINTED_VALUE,
} from './inspect';
export {BACKEND_RESPONSES_LOG_MESSAGE, LogEventStatus, LogEventType} from './log';
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type {
Url,
} from './http';
export type {KeyboardPressKey} from './keyboard';
export type {Log, LogContext, LogParams, LogPayload, LogTag} from './log';
export type {Log, LogContext, LogParams, LogPayload, LogTag, ValueToStringOptions} from './log';
export type {
Dimensions,
DimensionsString,
Expand Down
1 change: 1 addition & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type {
MapLogPayload,
MapLogPayloadInReport,
Payload,
ValueToStringOptions,
} from './log';
export type {
Dimensions,
Expand Down
8 changes: 8 additions & 0 deletions src/types/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {InspectOptions} from 'node:util';

import type {LogEventStatus, LogEventType} from '../constants/internal';

import type {ResponseWithRequest} from './http';
Expand Down Expand Up @@ -32,6 +34,7 @@ export type LogPayload = Readonly<{
filePath?: unknown;
logEventStatus?: LogEventStatus;
logTag?: LogTag;
maxLinesCountInPrintedValue?: number;
successful?: unknown;
}> &
Payload;
Expand Down Expand Up @@ -77,3 +80,8 @@ export type MapLogPayloadInReport = (
* Object with some payload.
*/
export type Payload = Readonly<Record<string, unknown>>;

/**
* Options of `valueToString` function.
*/
export type ValueToStringOptions = Readonly<InspectOptions & {maxLines?: number}>;
2 changes: 1 addition & 1 deletion src/utils/events/registerStartE2edRunEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const registerStartE2edRunEvent = async (): Promise<void> => {
const isLocalRun = runEnvironment !== RunEnvironment.Docker;
const startMessage = `Run tests ${isLocalRun ? 'local' : 'in docker'} with e2ed@${e2ed.version}`;

generalLog(startMessage, startInfo);
generalLog(startMessage, {...startInfo, maxLinesCountInPrintedValue: 1024});
} finally {
await writeStartInfo(startInfo);
await writeLogsToFile();
Expand Down
16 changes: 13 additions & 3 deletions src/utils/generalLog/getLogMessageBody.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {CONSOLE_INSPECT_OPTIONS} from '../../constants/internal';
import {CONSOLE_INSPECT_OPTIONS, DEFAULT_INSPECT_OPTIONS} from '../../constants/internal';

import {valueToString} from '../valueToString';

import type {LogContext, LogPayload} from '../../types/internal';
import type {LogContext, LogPayload, ValueToStringOptions} from '../../types/internal';

/**
* Get body of log message by context, isLogInConsole flag and log payload.
Expand All @@ -20,5 +20,15 @@ export const getLogMessageBody = (
return '';
}

return ` ${valueToString(printedPayload, isLogInConsole ? CONSOLE_INSPECT_OPTIONS : undefined)}`;
const maxLines = payload == null ? undefined : payload.maxLinesCountInPrintedValue;

let options: ValueToStringOptions = isLogInConsole
? CONSOLE_INSPECT_OPTIONS
: DEFAULT_INSPECT_OPTIONS;

if (maxLines !== undefined) {
options = {...options, maxLines};
}

return ` ${valueToString(printedPayload, options)}`;
};
11 changes: 7 additions & 4 deletions src/utils/valueToString/getLinesArrayTrimmedToMaxLength.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';
import {DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';

const additionalLinesForRepeatedTrimmerRuns = 4;

/**
* Get lines array trimmed to max lines count in printed value.
* @internal
*/
export const getLinesArrayTrimmedToMaxLength = (lines: readonly string[]): readonly string[] => {
if (lines.length <= MAX_LINES_COUNT_IN_PRINTED_VALUE + additionalLinesForRepeatedTrimmerRuns) {
export const getLinesArrayTrimmedToMaxLength = (
lines: readonly string[],
maxLines = DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
): readonly string[] => {
if (lines.length <= maxLines + additionalLinesForRepeatedTrimmerRuns) {
return lines;
}

const halfOfLines = Math.floor(MAX_LINES_COUNT_IN_PRINTED_VALUE / 2);
const halfOfLines = Math.floor(maxLines / 2);
const cuttedLinesCount = lines.length - 2 * halfOfLines;

return [
Expand Down
24 changes: 20 additions & 4 deletions src/utils/valueToString/valueToString.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import {inspect, type InspectOptions} from 'node:util';
import {inspect} from 'node:util';

import {DEFAULT_INSPECT_OPTIONS, MAX_LINES_COUNT_IN_PRINTED_VALUE} from '../../constants/internal';
import {
DEFAULT_INSPECT_OPTIONS,
DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE,
} from '../../constants/internal';

import {cutVerboseLinesFromPrintedLines} from './cutVerboseLinesFromPrintedLines';
import {getLinesArrayTrimmedToMaxLength} from './getLinesArrayTrimmedToMaxLength';
import {getStringTrimmedToMaxLength} from './getStringTrimmedToMaxLength';

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

/**
* Returns string presentation of arbitrary value.
*/
export const valueToString = (
value: unknown,
options: InspectOptions = DEFAULT_INSPECT_OPTIONS,
options: ValueToStringOptions = DEFAULT_INSPECT_OPTIONS,
): string => {
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES_COUNT_IN_PRINTED_VALUE;

if (value instanceof Error) {
for (const symbol of Object.getOwnPropertySymbols(value)) {
// We remove symbol fields from Playwright error objects, since printing them creates gigantic logs.
// @ts-expect-error: cannot set symbol to Error
// eslint-disable-next-line no-param-reassign
value[symbol] = {};
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a comment here explaining why we’re doing this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I added a comment about that.


const valueAsString = inspect(value, options);
const lines = valueAsString.split('\n');

if (lines.length <= MAX_LINES_COUNT_IN_PRINTED_VALUE) {
if (lines.length <= maxLines) {
return getStringTrimmedToMaxLength(valueAsString);
}

Expand Down
Loading