Skip to content

Commit

Permalink
Add more extensive types.
Browse files Browse the repository at this point in the history
  • Loading branch information
archfz committed Sep 14, 2024
1 parent 263aa97 commit d517252
Show file tree
Hide file tree
Showing 35 changed files with 570 additions and 680 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ test/cypress/reports
.vscode
.aider*
src/**/*.js
src/**/*.d.ts
2 changes: 0 additions & 2 deletions src/CtrError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export default class CtrError extends Error {

constructor(message: any) {
super(`cypress-terminal-report: ${message}`);
}

}
12 changes: 8 additions & 4 deletions src/collector/LogCollectBaseControl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import CtrError from '../CtrError';
import {ExtendedSupportOptions} from "../installLogsCollector.types";
import LogCollectorState from "./LogCollectorState";
import {TestData} from "../types";

export default class LogCollectBaseControl {
collectorState: any;
config: any;
prepareLogs(logStackIndex: any, testData: any) {
export default abstract class LogCollectBaseControl {
protected abstract collectorState: LogCollectorState;
protected abstract config: ExtendedSupportOptions;

prepareLogs(logStackIndex: number, testData: TestData) {
let logsCopy = this.collectorState.consumeLogStacks(logStackIndex);

if (logsCopy === null) {
Expand Down
28 changes: 14 additions & 14 deletions src/collector/LogCollectBrowserConsole.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import CONSTANTS from '../constants';
import utils from '../utils';
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";
import {Severity} from "../types";

export default class LogCollectBrowserConsole {
collectorState: any;
config: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
}
constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {}

register() {
const oldConsoleMethods = {};
Expand All @@ -17,16 +14,15 @@ export default class LogCollectBrowserConsole {
: 'window:before:load';

Cypress.on(event, () => {
const docIframe = window.parent.document.querySelector("[id*='Your project: ']") ||
window.parent.document.querySelector("[id*='Your App']");
// @ts-expect-error TS(2531): Object is possibly 'null'.
const appWindow = docIframe.contentWindow;
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;

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

const processArg = (arg: any) => {
if (['string', 'number', 'undefined', 'function'].includes(typeof arg)) {
Expand All @@ -47,7 +43,11 @@ export default class LogCollectBrowserConsole {
return utils.jsonStringify(arg);
};

const createWrapper = (method: any, logType: any, type = CONSTANTS.SEVERITY.SUCCESS) => {
const createWrapper = (
method: 'warn' | 'error' | 'debug' | 'info' | 'log',
logType: any,
type: Severity = CONSTANTS.SEVERITY.SUCCESS
) => {
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
oldConsoleMethods[method] = appWindow.console[method];

Expand Down
17 changes: 7 additions & 10 deletions src/collector/LogCollectCypressCommand.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import CONSTANTS from '../constants';
import utils from '../utils';
import LogFormat from "./LogFormat";
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

export default class LogCollectCypressCommand {
collectorState: any;
config: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
}
constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {}

register() {
const isOfInterest = (options: any) => options.instrument === 'command' &&
Expand All @@ -27,15 +24,15 @@ export default class LogCollectCypressCommand {
return message;
};

Cypress.on('log:added', (options: any) => {
Cypress.on('log:added', (options) => {
if (isOfInterest(options)) {
const log = formatLogMessage(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.ERROR : '';
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.ERROR : CONSTANTS.SEVERITY.SUCCESS;
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_COMMAND, log, severity], options.id);
}
});

Cypress.on('log:changed', (options: any) => {
Cypress.on('log:changed', (options) => {
if (isOfInterest(options)) {
const log = formatLogMessage(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.ERROR : CONSTANTS.SEVERITY.SUCCESS;
Expand Down
19 changes: 8 additions & 11 deletions src/collector/LogCollectCypressFetch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import CONSTANTS from '../constants';
import LogFormat from "./LogFormat";
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

export default class LogCollectCypressFetch {
collectorState: any;
config: any;
format: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
format: LogFormat;

constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {
this.format = new LogFormat(config);
}

Expand All @@ -21,17 +18,17 @@ export default class LogCollectCypressFetch {
(consoleProps(options)["Request went to origin?"] !== 'yes' ? 'STUBBED ' : '') +
consoleProps(options).Method + ' ' + consoleProps(options).URL;

const formatDuration = (durationInMs: any) => durationInMs < 1000 ? `${durationInMs} ms` : `${durationInMs / 1000} s`;
const formatDuration = (durationInMs: number) => durationInMs < 1000 ? `${durationInMs} ms` : `${durationInMs / 1000} s`;

Cypress.on('log:added', (options: any) => {
Cypress.on('log:added', (options) => {
if (options.instrument === 'command' && options.name === 'request' && options.displayName === 'fetch') {
const log = formatFetch(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.WARNING : '';
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.WARNING : CONSTANTS.SEVERITY.SUCCESS;
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_FETCH, log, severity], options.id);
}
});

Cypress.on('log:changed', async (options: any) => {
Cypress.on('log:changed', async (options) => {
if (
options.instrument === 'command' && options.name === 'request' && options.displayName === 'fetch' &&
options.state !== 'pending'
Expand Down
13 changes: 5 additions & 8 deletions src/collector/LogCollectCypressIntercept.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import CONSTANTS from '../constants';
import LogFormat from "./LogFormat";
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

Object.defineProperty(RegExp.prototype, "toJSON", {
value: RegExp.prototype.toString
});

export default class LogCollectCypressIntercept {
collectorState: any;
config: any;
format: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
format: LogFormat;

constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {
this.format = new LogFormat(config);
}

register() {
Cypress.Commands.overwrite('intercept', (originalFn: any, ...args: any[]) => {
Cypress.Commands.overwrite('intercept', (originalFn, ...args) => {
let message = '';

if (typeof args[0] === "string" && CONSTANTS.HTTP_METHODS.includes(args[0].toUpperCase())) {
Expand Down
14 changes: 5 additions & 9 deletions src/collector/LogCollectCypressLog.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import CONSTANTS from '../constants';
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

export default class LogCollectCypressLog {
collectorState: any;
config: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
}
constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {}

register() {
Cypress.Commands.overwrite('log', (subject: any, ...args: any[]) => {
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_LOG, args.join(' ')]);
Cypress.Commands.overwrite('log', (subject, ...args) => {
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_LOG, args.join(' '), CONSTANTS.SEVERITY.SUCCESS]);
subject(...args);
});
}
Expand Down
19 changes: 8 additions & 11 deletions src/collector/LogCollectCypressRequest.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import CONSTANTS from '../constants';
import LogFormat from "./LogFormat";
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

export default class LogCollectCypressRequest {
collectorState: any;
config: any;
format: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
format: LogFormat;

constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {
this.format = new LogFormat(config);
}

register() {
const isValidHttpMethod = (str: any) => typeof str === 'string' && CONSTANTS.HTTP_METHODS.some((s: any) => str.toUpperCase().includes(s));
const isValidHttpMethod = (str: any) => typeof str === 'string' && CONSTANTS.HTTP_METHODS.some((s) => str.toUpperCase().includes(s));

const isNetworkError = (e: any) => e.message && e.message.startsWith('`cy.request()` failed trying to load:');

const isStatusCodeFailure = (e: any) => e.message && e.message.startsWith('`cy.request()` failed on:');

const parseRequestStatusCodeFailureMessage = (message: any) => {
const parseRequestStatusCodeFailureMessage = (message: string) => {
const responseStart = '\n\nThe response we got was:\n\n';
const statusStart = 'Status: ';
const headersStart = '\nHeaders: ';
Expand All @@ -46,7 +43,7 @@ export default class LogCollectCypressRequest {
return {status: statusStr, headers: headersStr, body: bodyStr.trimEnd()};
};

const parseRequestNetworkError = (message: any) => {
const parseRequestNetworkError = (message: string) => {
const errorPartStart = 'We received this error at the network level:\n\n > ';
const errorPrefix = 'Error: ';
if (message.indexOf(errorPartStart) === -1) {
Expand Down Expand Up @@ -138,7 +135,7 @@ export default class LogCollectCypressRequest {
},
});

this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_REQUEST, log]);
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_REQUEST, log, CONSTANTS.SEVERITY.SUCCESS]);
return response;
});
});
Expand Down
17 changes: 7 additions & 10 deletions src/collector/LogCollectCypressXhr.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import CONSTANTS from '../constants';
import LogFormat from "./LogFormat";
import LogCollectorState from "./LogCollectorState";
import {ExtendedSupportOptions} from "../installLogsCollector.types";

export default class LogCollectCypressXhr {
collectorState: any;
config: any;
format: any;

constructor(collectorState: any, config: any) {
this.config = config;
this.collectorState = collectorState;
format: LogFormat;

constructor(protected collectorState: LogCollectorState, protected config: ExtendedSupportOptions) {
this.format = new LogFormat(config);
}

Expand All @@ -22,19 +19,19 @@ export default class LogCollectCypressXhr {

const formatDuration = (durationInMs: any) => durationInMs < 1000 ? `${durationInMs} ms` : `${durationInMs / 1000} s`;

Cypress.on('log:added', (options: any) => {
Cypress.on('log:added', (options) => {
if (
options.instrument === 'command' &&
consoleProps(options) &&
options.displayName === 'xhr'
) {
const log = formatXhr(options);
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.WARNING : '';
const severity = options.state === 'failed' ? CONSTANTS.SEVERITY.WARNING : CONSTANTS.SEVERITY.SUCCESS;
this.collectorState.addLog([CONSTANTS.LOG_TYPES.CYPRESS_XHR, log, severity], options.id);
}
});

Cypress.on('log:changed', async (options: any) => {
Cypress.on('log:changed', async (options) => {
if (
options.instrument === 'command' &&
['request', 'xhr'].includes(options.name) &&
Expand Down
Loading

0 comments on commit d517252

Please sign in to comment.