Skip to content

Commit

Permalink
Merge pull request #261 from bvandercar-vt/bvandercar/more-constants
Browse files Browse the repository at this point in the history
refactor: more constants, improved types
  • Loading branch information
archfz authored Nov 13, 2024
2 parents 83c16dd + 911541a commit 0b2956d
Show file tree
Hide file tree
Showing 29 changed files with 376 additions and 310 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ test
*.png
src/**/*.ts
!src/**/*.d.ts
*.internal.*
tsconfig.json
10 changes: 4 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
declare namespace Cypress {
import {Log as CtrLog} from "./src/installLogsCollector";
import type {Log} from "./src/installLogsCollector";
import type {BuiltinOutputProcessorsTypes} from "./src/types";

interface Cypress {
TerminalReport: {
getLogs<T extends 'txt' | 'json' | 'none' = 'none'>(format?: T): {
txt: string,
json: string,
none: CtrLog[],
}[T];
getLogs(format: BuiltinOutputProcessorsTypes): string | null;
getLogs(format?: 'none' = 'none'): Log[] | null;
}
}
}
50 changes: 50 additions & 0 deletions index.internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* This file used for internally adding types to package interfaces that are not
* documented by the package (not included in the package's types).
*/

declare namespace Cypress {
interface Cypress {
onSpecReady(...args: any[]): void;

mocha: {
getRunner(): Mocha.Runner
getRootSuite(): Mocha.Suite
}

state(key: 'done', callback: (error: Error) => Promise<void>): void
state(key: 'error', error: Error): void
}

interface Chainable<Subject = any> extends Pick<Promise<Subject>, 'catch'> {
}
}

declare namespace Mocha {
interface InvocationDetails {
relativeFile?: string
fileUrl?: string
}

interface Hook {
hookName: string
_ctr_hook: boolean
}

interface Runnable {
hookName: string
invocationDetails: InvocationDetails
id: string
order: unknown
wallClockStartedAt: unknown
timings: unknown
}

interface Suite {
invocationDetails: InvocationDetails
}

interface Test {
failedFromHookId?: unknown
}
}
4 changes: 2 additions & 2 deletions package-lock.json

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

36 changes: 15 additions & 21 deletions src/collector/LogCollectBrowserConsole.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import CONSTANTS from '../constants';
import utils from '../utils';
import LogCollectorState from "./LogCollectorState";
import type {ExtendedSupportOptions} from "../installLogsCollector.types";
import type {Severity} from "../types";
import type {LogType, Severity} from "../types";
import LogCollectBase from "./LogCollectBase";

type Methods = 'warn' | 'error' | 'debug' | 'info' | 'log';
Expand All @@ -17,13 +15,13 @@ export default class LogCollectBrowserConsole extends LogCollectBase {
Cypress.on(event, () => {
const docIframe = (window.parent.document.querySelector("[id*='Your project: ']") ||
window.parent.document.querySelector("[id*='Your App']")) as HTMLIFrameElement;
const appWindow = docIframe.contentWindow as Window & typeof globalThis;
const appWindow = docIframe.contentWindow as Window & typeof globalThis & {_ctr_registered: boolean};

// In case of component tests the even will be called multiple times. Prevent registering multiple times.
if (!appWindow || (appWindow as any)._ctr_registered) {
if (!appWindow || appWindow._ctr_registered) {
return;
}
(appWindow as any)._ctr_registered = true;
appWindow._ctr_registered = true;

const stringableTypes = ['string', 'number', 'undefined', 'function'];
const processArg = (arg: any) => {
Expand All @@ -47,7 +45,7 @@ export default class LogCollectBrowserConsole extends LogCollectBase {

const createWrapper = (
method: Methods,
logType: any,
logType: LogType,
type: Severity = CONSTANTS.SEVERITY.SUCCESS
) => {
oldConsoleMethods[method] = appWindow.console[method];
Expand All @@ -60,20 +58,16 @@ export default class LogCollectBrowserConsole extends LogCollectBase {
};
};

if (this.config.collectTypes.includes(CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_WARN)) {
createWrapper('warn', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_WARN, CONSTANTS.SEVERITY.WARNING);
}
if (this.config.collectTypes.includes(CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_ERROR)) {
createWrapper('error', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_ERROR, CONSTANTS.SEVERITY.ERROR);
}
if (this.config.collectTypes.includes(CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_INFO)) {
createWrapper('info', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_INFO);
}
if (this.config.collectTypes.includes(CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_DEBUG)) {
createWrapper('debug', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_DEBUG);
}
if (this.config.collectTypes.includes(CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_LOG)) {
createWrapper('log', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_LOG);
for (const [method, logType, severity] of [
['warn', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_WARN, CONSTANTS.SEVERITY.WARNING],
['error', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_ERROR, CONSTANTS.SEVERITY.ERROR],
['info', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_INFO],
['debug', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_DEBUG],
['log', CONSTANTS.LOG_TYPES.BROWSER_CONSOLE_LOG],
] as const) {
if (this.config.collectTypes.includes(logType)) {
createWrapper(method, logType, severity);
}
}
});
}
Expand Down
39 changes: 16 additions & 23 deletions src/collector/LogCollectControlBase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CtrError from '../CtrError';
import type {ExtendedSupportOptions} from "../installLogsCollector.types";
import LogCollectorState from "./LogCollectorState";
import type {MessageData, TestData} from "../types";
import type {MessageData, SetOptional, State, TestData} from "../types";

export default abstract class LogCollectControlBase {
protected abstract collectorState: LogCollectorState;
Expand All @@ -11,7 +11,7 @@ export default abstract class LogCollectControlBase {
logStackIndex: number,
mochaRunnable: Mocha.Runnable,
options: {
state?: string,
state?: State,
title?: string,
noQueue?: boolean,
consoleTitle?: string,
Expand All @@ -20,34 +20,30 @@ export default abstract class LogCollectControlBase {
continuous?: boolean,
} = {}
) {
let testState: MessageData['state'] = (options.state || mochaRunnable.state) as MessageData['state'] || 'running';
let testState = options.state || mochaRunnable.state;
let testTitle = options.title || mochaRunnable.title;
let testLevel = 0;

let spec = this.getSpecFilePath(mochaRunnable);

if (!spec) {
return;
}
if (!spec) return;

let wait = typeof options.wait === 'number' ? options.wait : 5;

{
let parent = mochaRunnable.parent;
while (parent && parent.title) {
while (parent?.title) {
testTitle = `${parent.title} -> ${testTitle}`
parent = parent.parent;
++testLevel;
}
}

if (testState === 'failed' && mochaRunnable && (mochaRunnable as any)._retries > 0) {
testTitle += ` (Attempt ${mochaRunnable && (mochaRunnable as any)._currentRetry + 1})`
if (testState === 'failed' && mochaRunnable && mochaRunnable["_retries"] > 0) {
testTitle += ` (Attempt ${mochaRunnable && mochaRunnable["_currentRetry"] + 1})`
}

const prepareLogs = () => {
return this.prepareLogs(logStackIndex, {mochaRunnable, testState, testTitle, testLevel});
};
const prepareLogs = () =>
this.prepareLogs(logStackIndex, {mochaRunnable, testState, testTitle, testLevel});

const buildDataMessage = () => ({
spec: spec,
Expand All @@ -63,7 +59,7 @@ export default abstract class LogCollectControlBase {
this.triggerSendTask(buildDataMessage, options.noQueue || false, wait);
}

protected abstract triggerSendTask(buildDataMessage: (continuous?: boolean) => MessageData, noQueue: boolean, wait: number): void;
protected abstract triggerSendTask(buildDataMessage: (continuous?: boolean) => SetOptional<MessageData, 'state'>, noQueue: boolean, wait: number): void;

prepareLogs(logStackIndex: number, testData: TestData) {
let logsCopy = this.collectorState.consumeLogStacks(logStackIndex);
Expand All @@ -87,24 +83,21 @@ export default abstract class LogCollectControlBase {
return logsCopy;
}

getSpecFilePath(mochaRunnable: any) {
if (!mochaRunnable.invocationDetails && !mochaRunnable.parent.invocationDetails) {
if (mochaRunnable.parent.file) {
return mochaRunnable.parent.file;
}
return null;
getSpecFilePath(mochaRunnable: Mocha.Runnable) {
if (!mochaRunnable.invocationDetails && !mochaRunnable.parent?.invocationDetails) {
return mochaRunnable.parent?.file ?? null;
}

let invocationDetails = mochaRunnable.invocationDetails;
let parent = mochaRunnable.parent;
// always get top-most spec to determine the called .spec file
while (parent && parent.invocationDetails) {
while (parent?.invocationDetails) {
invocationDetails = parent.invocationDetails
parent = parent.parent;
}

return parent.file || // Support for cypress-grep.
return parent?.file || // Support for cypress-grep.
invocationDetails.relativeFile ||
(invocationDetails.fileUrl && invocationDetails.fileUrl.replace(/^[^?]+\?p=/, ''));
invocationDetails.fileUrl?.replace(/^[^?]+\?p=/, '');
}
}
Loading

0 comments on commit 0b2956d

Please sign in to comment.