Skip to content

Commit

Permalink
fix defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandercar-vt committed Nov 20, 2024
1 parent 2e4682e commit 57bccbc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/installLogsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function installLogsCollector(config: SupportOptions = {}) {
validateConfig(config);

const extendedConfig: ExtendedSupportOptions = {
collectTypes: Object.values(CONSTANTS.LOG_TYPES),
...config,
collectTypes: config.collectTypes || Object.values(CONSTANTS.LOG_TYPES),
collectBody: config.xhr?.printBody ?? true,
collectRequestData: config.xhr?.printRequestData,
collectHeaderData: config.xhr?.printHeaderData,
Expand Down
65 changes: 40 additions & 25 deletions src/installLogsPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,35 @@ const createLogger = (enabled?: boolean) =>
* @type {import('./installLogsPrinter')}
*/
function installLogsPrinter(on: Cypress.PluginEvents, options: PluginOptions = {}) {
options.printLogsToFile = options.printLogsToFile || 'onFail';
options.printLogsToConsole = options.printLogsToConsole || 'onFail';
const [error] = validate(options, InstallLogsPrinterSchema);
const resolvedOptions: PluginOptions = {
printLogsToFile: 'onFail',
printLogsToConsole: 'onFail',
routeTrimLength: 5000,
defaultTrimLength: 800,
commandTrimLength: 800,
outputVerbose: true,
...options,
};

const {
printLogsToFile,
printLogsToConsole,
outputCompactLogs,
outputTarget,logToFilesOnAfterRun,
includeSuccessfulHookLogs,
compactLogs: compactLogsOption,
collectTestLogs,
} = resolvedOptions;

const [error] = validate(resolvedOptions, InstallLogsPrinterSchema);

if (error) {
throw new CtrError(
`Invalid plugin install options: ${utils.validatorErrToStr(error.failures())}`
);
}

const logDebug = createLogger(options.debug);
const logDebug = createLogger(resolvedOptions.debug);

on('task', {
[CONSTANTS.TASK_NAME]: function (data: MessageData) {
Expand All @@ -62,19 +80,19 @@ function installLogsPrinter(on: Cypress.PluginEvents, options: PluginOptions = {
let messages = data.messages;

const terminalMessages =
typeof options.compactLogs === 'number' && options.compactLogs >= 0
? compactLogs(messages, options.compactLogs, logDebug)
typeof compactLogsOption === 'number' && compactLogsOption >= 0
? compactLogs(messages, compactLogsOption, logDebug)
: messages;

const isHookAndShouldLog =
data.isHook && (options.includeSuccessfulHookLogs || data.state === 'failed');
data.isHook && (includeSuccessfulHookLogs || data.state === 'failed');

if (options.outputTarget && options.printLogsToFile !== 'never') {
if (data.state === 'failed' || options.printLogsToFile === 'always' || isHookAndShouldLog) {
if (outputTarget && printLogsToFile !== 'never') {
if (data.state === 'failed' || printLogsToFile === 'always' || isHookAndShouldLog) {
let outputFileMessages =
typeof options.outputCompactLogs === 'number'
? compactLogs(messages, options.outputCompactLogs, logDebug)
: options.outputCompactLogs === false
typeof outputCompactLogs === 'number'
? compactLogs(messages, outputCompactLogs, logDebug)
: outputCompactLogs === false
? messages
: terminalMessages;

Expand All @@ -88,42 +106,39 @@ function installLogsPrinter(on: Cypress.PluginEvents, options: PluginOptions = {
}

if (
options.printLogsToConsole !== 'never' &&
(options.printLogsToConsole === 'always' ||
(options.printLogsToConsole === 'onFail' && data.state !== 'passed') ||
printLogsToConsole !== 'never' &&
(printLogsToConsole === 'always' ||
(printLogsToConsole === 'onFail' && data.state !== 'passed') ||
isHookAndShouldLog)
) {
logDebug(
`Logging to console ${terminalMessages.length} messages, for ${data.spec}:${data.test}.`
);
consoleProcessor(terminalMessages, options, data);
consoleProcessor(terminalMessages, resolvedOptions, data);
}

if (options.collectTestLogs) {
if (collectTestLogs) {
logDebug(
`Running \`collectTestLogs\` on ${terminalMessages.length} messages, for ${data.spec}:${data.test}.`
);
options.collectTestLogs(
{spec: data.spec, test: data.test, state: data.state},
terminalMessages
);
collectTestLogs({spec: data.spec, test: data.test, state: data.state}, terminalMessages);
}

return null;
},
[CONSTANTS.TASK_NAME_OUTPUT]: () => {
logDebug(`${CONSTANTS.TASK_NAME_OUTPUT}: Triggered.`);
logToFiles(options);
logToFiles(resolvedOptions);
return null;
},
});

installOutputProcessors(on, options);
installOutputProcessors(on, resolvedOptions);

if (options.logToFilesOnAfterRun) {
if (logToFilesOnAfterRun) {
on('after:run', () => {
logDebug(`after:run: Attempting file logging on after run.`);
logToFiles(options);
logToFiles(resolvedOptions);
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/outputProcessor/consoleProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ const TYPE_COMPUTE: Record<
[LOG_TYPES.CYPRESS_XHR]: (options) => ({
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength || 5000,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_FETCH]: (options) => ({
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength || 5000,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_INTERCEPT]: (options) => ({
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength || 5000,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_REQUEST]: (options) => ({
color: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
trim: options.routeTrimLength || 5000,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_COMMAND]: (options) => ({
color: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
trim: options.routeTrimLength || 5000,
trim: options.routeTrimLength,
}),
};

Expand Down Expand Up @@ -111,7 +111,7 @@ function consoleProcessor(messages: Log[], options: PluginOptions, data: Message

messages.forEach(({type, message, severity, timeString}) => {
let processedMessage = message;
let {color, icon, trim = options.defaultTrimLength || 800} = TYPE_COMPUTE[type](options);
let {color, icon, trim = options.defaultTrimLength} = TYPE_COMPUTE[type](options);

if (severity === CONSTANTS.SEVERITY.ERROR) {
color = COLORS.RED;
Expand All @@ -122,7 +122,7 @@ function consoleProcessor(messages: Log[], options: PluginOptions, data: Message
}

const maybeTrimLength = (msg: string) =>
msg.length > trim ? msg.substring(0, trim) + ' ...' : msg;
trim && msg.length > trim ? msg.substring(0, trim) + ' ...' : msg;

if (type == 'cy:log') {
processedMessage = utils.applyMessageMarkdown(processedMessage, {
Expand Down

0 comments on commit 57bccbc

Please sign in to comment.