cypress-terminal-report
Version:
Better terminal and file output for cypress test logs.
102 lines (101 loc) • 5.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const CtrError_1 = __importDefault(require("./CtrError"));
const LogCollectBrowserConsole_1 = __importDefault(require("./collector/LogCollectBrowserConsole"));
const LogCollectCypressCommand_1 = __importDefault(require("./collector/LogCollectCypressCommand"));
const LogCollectCypressRequest_1 = __importDefault(require("./collector/LogCollectCypressRequest"));
const LogCollectCypressIntercept_1 = __importDefault(require("./collector/LogCollectCypressIntercept"));
const LogCollectCypressBrowserNetwork_1 = __importDefault(require("./collector/LogCollectCypressBrowserNetwork"));
const LogCollectCypressLog_1 = __importDefault(require("./collector/LogCollectCypressLog"));
const LogCollectorState_1 = __importDefault(require("./collector/LogCollectorState"));
const LogCollectControlExtended_1 = __importDefault(require("./collector/LogCollectControlExtended"));
const LogCollectControlSimple_1 = __importDefault(require("./collector/LogCollectControlSimple"));
const logsTxtFormatter_1 = __importDefault(require("./outputProcessor/logsTxtFormatter"));
const constants_1 = __importDefault(require("./constants"));
const utils_1 = __importDefault(require("./utils"));
const superstruct_1 = require("superstruct");
const installLogsCollector_schema_1 = require("./installLogsCollector.schema");
const compare_versions_1 = require("compare-versions");
/**
* Installs the logs collector for cypress. Needs to be added to support file.
*/
function installLogsCollector(config = {}) {
var _a, _b, _c, _d;
validateConfig(config);
const extendedConfig = {
collectTypes: Object.values(constants_1.default.LOG_TYPES),
...config,
collectBody: (_b = (_a = config.xhr) === null || _a === void 0 ? void 0 : _a.printBody) !== null && _b !== void 0 ? _b : true,
collectRequestData: (_c = config.xhr) === null || _c === void 0 ? void 0 : _c.printRequestData,
collectHeaderData: (_d = config.xhr) === null || _d === void 0 ? void 0 : _d.printHeaderData,
maxLogLength: typeof config.maxLogLength === 'number' ? config.maxLogLength : 15000,
};
let logCollectorState = new LogCollectorState_1.default(extendedConfig);
registerLogCollectorTypes(logCollectorState, extendedConfig);
if (extendedConfig.enableExtendedCollector) {
new LogCollectControlExtended_1.default(logCollectorState, extendedConfig).register();
}
else {
new LogCollectControlSimple_1.default(logCollectorState, extendedConfig).register();
}
registerGlobalApi(logCollectorState);
}
function registerLogCollectorTypes(logCollectorState, config) {
new LogCollectBrowserConsole_1.default(logCollectorState, config).register();
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_LOG)) {
new LogCollectCypressLog_1.default(logCollectorState, config).register();
}
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_XHR)) {
new LogCollectCypressBrowserNetwork_1.default('xhr', logCollectorState, config).register();
}
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_FETCH)) {
new LogCollectCypressBrowserNetwork_1.default('fetch', logCollectorState, config).register();
}
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_REQUEST)) {
new LogCollectCypressRequest_1.default(logCollectorState, config).register();
}
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_COMMAND)) {
new LogCollectCypressCommand_1.default(logCollectorState, config).register();
}
if (config.collectTypes.includes(constants_1.default.LOG_TYPES.CYPRESS_INTERCEPT) &&
(0, compare_versions_1.compare)(Cypress.version, '6.0.0', '>=')) {
new LogCollectCypressIntercept_1.default(logCollectorState, config).register();
}
}
function registerGlobalApi(logCollectorState) {
Cypress.TerminalReport = {
//@ts-ignore there is no error, this works correctly.
getLogs: (format = 'none') => {
const logs = logCollectorState.getCurrentLogStack();
if (!logs) {
return null;
}
switch (format) {
case 'txt':
return (0, logsTxtFormatter_1.default)(logs);
case 'json':
return JSON.stringify(logs, null, 2);
default:
return logs;
}
},
};
}
function validateConfig(config) {
const [error] = (0, superstruct_1.validate)(config, installLogsCollector_schema_1.InstallLogsCollectorSchema);
if (error) {
throw new CtrError_1.default(`Invalid plugin install options: ${utils_1.default.validatorErrToStr(error.failures())}`);
}
if (config.filterLog && typeof config.filterLog !== 'function') {
throw new CtrError_1.default(`Filter log option expected to be a function.`);
}
if (config.processLog && typeof config.processLog !== 'function') {
throw new CtrError_1.default(`Process log option expected to be a function.`);
}
if (config.collectTestLogs && typeof config.collectTestLogs !== 'function') {
throw new CtrError_1.default(`Collect test logs option expected to be a function.`);
}
}
module.exports = installLogsCollector;