cypress-terminal-report
Version:
Better terminal and file output for cypress test logs.
61 lines (60 loc) • 3.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = __importDefault(require("../constants"));
const utils_1 = __importDefault(require("../utils"));
const LogCollectBase_1 = __importDefault(require("./LogCollectBase"));
class LogCollectBrowserConsole extends LogCollectBase_1.default {
register() {
const oldConsoleMethods = {};
const event = Cypress.testingType === 'component' ? 'test:before:run' : 'window:before:load';
Cypress.on(event, () => {
const docIframe = (window.parent.document.querySelector("[id*='Your project: ']") ||
window.parent.document.querySelector("[id*='Your App']"));
const appWindow = docIframe.contentWindow;
// In case of component tests the even will be called multiple times. Prevent registering multiple times.
if (!appWindow || appWindow._ctr_registered) {
return;
}
appWindow._ctr_registered = true;
const stringableTypes = ['string', 'number', 'undefined', 'function'];
const processArg = (arg) => {
if (stringableTypes.includes(typeof arg)) {
return arg ? arg.toString() : arg === undefined ? 'undefined' : '';
}
if ((arg instanceof appWindow.Error || arg instanceof Error) &&
typeof arg.stack === 'string') {
let stack = arg.stack;
if (stack.indexOf(arg.message) !== -1) {
stack = stack.slice(stack.indexOf(arg.message) + arg.message.length + 1);
}
return arg.toString() + '\n' + stack;
}
return utils_1.default.jsonStringify(arg);
};
const createWrapper = (method, logType, type = constants_1.default.SEVERITY.SUCCESS) => {
oldConsoleMethods[method] = appWindow.console[method];
appWindow.console[method] = (...args) => {
this.collectorState.addLog([logType, args.map(processArg).join(`,\n`), type]);
if (oldConsoleMethods[method]) {
oldConsoleMethods[method](...args);
}
};
};
for (const [method, logType, severity] of [
['warn', constants_1.default.LOG_TYPES.BROWSER_CONSOLE_WARN, constants_1.default.SEVERITY.WARNING],
['error', constants_1.default.LOG_TYPES.BROWSER_CONSOLE_ERROR, constants_1.default.SEVERITY.ERROR],
['info', constants_1.default.LOG_TYPES.BROWSER_CONSOLE_INFO],
['debug', constants_1.default.LOG_TYPES.BROWSER_CONSOLE_DEBUG],
['log', constants_1.default.LOG_TYPES.BROWSER_CONSOLE_LOG],
]) {
if (this.config.collectTypes.includes(logType)) {
createWrapper(method, logType, severity);
}
}
});
}
}
exports.default = LogCollectBrowserConsole;