cypress-xray-plugin
Version:
A Cypress plugin for uploading test results to Xray (test management for Jira)
165 lines (164 loc) • 5.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOG = exports.CapturingLogger = exports.PluginLogger = void 0;
const ansi_colors_1 = __importDefault(require("ansi-colors"));
const axios_1 = require("axios");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const errors_1 = require("./errors");
const LOG_LEVELS = ["debug", "error", "info", "notice", "warning"];
/**
* An ANSI-based logger.
*/
class PluginLogger {
constructor(options = { logDirectory: "." }) {
this.loggingOptions = options;
const maxPrefixLength = Math.max(...LOG_LEVELS.map((s) => s.length));
this.prefixes = {
["debug"]: this.prefix("debug", maxPrefixLength),
["error"]: this.prefix("error", maxPrefixLength),
["info"]: this.prefix("info", maxPrefixLength),
["notice"]: this.prefix("notice", maxPrefixLength),
["warning"]: this.prefix("warning", maxPrefixLength),
};
this.colorizers = {
["debug"]: ansi_colors_1.default.cyan,
["error"]: ansi_colors_1.default.red,
["info"]: ansi_colors_1.default.gray,
["notice"]: ansi_colors_1.default.green,
["warning"]: ansi_colors_1.default.yellow,
};
this.logFunctions = {
["debug"]: console.debug,
["error"]: console.error,
["info"]: console.info,
["notice"]: console.log,
["warning"]: console.warn,
};
}
message(level, ...text) {
// Prefer custom logger to the default plugin one.
if (this.loggingOptions.logger) {
this.loggingOptions.logger(level, ...text);
return;
}
if (level === "debug" && !this.loggingOptions.debug) {
return;
}
const colorizer = this.colorizers[level];
const prefix = this.prefixes[level];
const logFunction = this.logFunctions[level];
const lines = text.join(" ").split("\n");
lines.forEach((line, index) => {
if (index === 0) {
logFunction(`${prefix} ${colorizer(line)}`);
}
else {
logFunction(`${prefix} ${colorizer(line)}`);
}
// Pad multiline log messages with an extra new line to cleanly separate them from the
// following line.
if (index > 0 && index === lines.length - 1) {
logFunction(prefix);
}
});
}
logToFile(data, filename) {
const logDirectoryPath = path_1.default.resolve(this.loggingOptions.logDirectory);
fs_1.default.mkdirSync(logDirectoryPath, { recursive: true });
const filepath = path_1.default.resolve(logDirectoryPath, filename);
fs_1.default.writeFileSync(filepath, data);
return filepath;
}
logErrorToFile(error, filename) {
var _a;
let errorFileName;
let errorData;
if ((0, errors_1.isLoggedError)(error)) {
return;
}
if ((0, axios_1.isAxiosError)(error)) {
errorFileName = `${filename}.json`;
errorData = {
error: error.toJSON(),
response: (_a = error.response) === null || _a === void 0 ? void 0 : _a.data,
};
}
else if (error instanceof Error) {
errorFileName = `${filename}.json`;
errorData = {
error: `${error.name}: ${error.message}`,
stacktrace: error.stack,
};
}
else {
errorFileName = `${filename}.log`;
errorData = error;
}
const filepath = this.logToFile(JSON.stringify(errorData, null, 2), errorFileName);
this.message("error", `Complete error logs have been written to: ${filepath}`);
}
configure(options) {
this.loggingOptions = options;
}
prefix(level, maxPrefixLength) {
return ansi_colors_1.default.white(`│ Cypress Xray Plugin │ ${level.toUpperCase().padEnd(maxPrefixLength, " ")} │`);
}
}
exports.PluginLogger = PluginLogger;
/**
* A logger which does not print anything itself but rather collects all log messages for later
* retrieval. Useful for testing purposes.
*/
class CapturingLogger {
constructor() {
this.messages = [];
this.fileLogMessages = [];
this.fileLogErrorMessages = [];
}
message(level, ...text) {
this.messages.push([level, ...text]);
}
/**
* Returns the captured log messages.
*
* @returns the log messages
*/
getMessages() {
return this.messages;
}
logToFile(data, filename) {
this.fileLogMessages.push([data, filename]);
return filename;
}
/**
* Returns the captured _log to file_ messages.
*
* @returns the _log to file_ messages
*/
getFileLogMessages() {
return this.fileLogMessages;
}
logErrorToFile(error, filename) {
this.fileLogErrorMessages.push([error, filename]);
}
/**
* Returns the captured _log error to file_ messages.
*
* @returns the _log error to file_ messages
*/
getFileLogErrorMessages() {
return this.fileLogErrorMessages;
}
configure() {
// Do nothing.
}
}
exports.CapturingLogger = CapturingLogger;
/**
* The global logger instance.
*/
exports.LOG = new PluginLogger();