@mmisty/cypress-allure-adapter
Version:
cypress allure adapter to generate allure results during tests execution (Allure TestOps compatible)
368 lines (367 loc) • 15.1 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.allureTasks = void 0;
const debug_1 = __importDefault(require("debug"));
const allure_reporter_plugin_1 = require("./allure-reporter-plugin");
const allure_types_1 = require("./allure-types");
const common_1 = require("../common");
const task_manager_1 = require("./task-manager");
const debug = (0, debug_1.default)('cypress-allure:proxy');
const log = (...args) => {
debug(args);
};
const allureTasks = (opts, client) => {
const taskManager = new task_manager_1.TaskManager();
taskManager.setClient(client);
let allureReporter = new allure_reporter_plugin_1.AllureReporter(opts, taskManager, client);
const allureResults = opts.allureResults;
const allureResultsWatch = opts.techAllureResults;
return {
taskManager,
specStarted: (arg) => {
log(`specStarted: ${JSON.stringify(arg)}`);
// reset state on spec start
allureReporter = new allure_reporter_plugin_1.AllureReporter(opts, taskManager, client);
allureReporter.specStarted(arg);
log('specStarted');
},
hookStarted: (arg) => {
log(`hookStart: ${JSON.stringify(arg)}`);
allureReporter.hookStarted(arg);
log('hookStarted');
},
hookEnded: (arg) => {
log(`hookEnd: ${JSON.stringify(arg)}`);
allureReporter.hookEnded(arg);
log('hookEnded');
},
suiteStarted: (arg) => {
log(`suiteStarted: ${JSON.stringify(arg)}`);
allureReporter.suiteStarted(arg);
log('suiteStarted');
},
stepStarted: (arg) => {
log(`stepStarted ${JSON.stringify(arg)}`);
allureReporter.startStep(arg);
log('stepStarted');
},
step: (arg) => {
var _a;
log(`step ${JSON.stringify(arg)}`);
allureReporter.startStep(arg);
allureReporter.endStep({ date: arg.date, status: (_a = arg.status) !== null && _a !== void 0 ? _a : allure_types_1.Status.PASSED, details: arg.details });
log('step');
},
mergeStepMaybe: (arg) => {
var _a, _b;
log(`mergePrevStep ${JSON.stringify(arg)}`);
const steps = (_b = (_a = allureReporter.currentTest) === null || _a === void 0 ? void 0 : _a.result.steps) !== null && _b !== void 0 ? _b : [];
const last = steps[(steps === null || steps === void 0 ? void 0 : steps.length) - 1];
if (last && arg.name === last.name) {
steps.splice((steps === null || steps === void 0 ? void 0 : steps.length) - 1, 1);
allureReporter.startStep({ name: arg.name, date: last.start });
// Add child steps from the merged step
if (allureReporter.currentStep) {
last.steps.forEach((s) => {
allureReporter.currentStep.result.steps.push(s);
});
}
}
else {
allureReporter.startStep({ name: arg.name, date: Date.now() });
}
log('mergePrevStep');
},
stepEnded: (arg) => {
log(`stepEnded ${JSON.stringify(arg)}`);
allureReporter.endStep(arg);
log('stepEnded');
},
endAllSteps: (arg) => {
log(`endAllSteps ${JSON.stringify(arg)}`);
allureReporter.endAllSteps(arg);
log('endAllSteps');
},
suiteEnded: (arg) => {
log(`suiteEnded ${JSON.stringify(arg)}`);
allureReporter.endGroup();
log('suiteEnded');
},
testStarted(arg) {
log(`testStarted ${JSON.stringify(arg)}`);
allureReporter.startTest(arg);
log('testStarted');
},
writeEnvironmentInfo(arg) {
log(`writeEnvironmentInfo ${JSON.stringify(arg)}`);
allureReporter.writeEnvironmentInfo(arg);
log('writeEnvironmentInfo');
},
addEnvironmentInfo(arg) {
return __awaiter(this, void 0, void 0, function* () {
log(`addEnvironmentInfo ${JSON.stringify(arg)}`);
allureReporter.addEnvironmentInfo(arg);
log('addEnvironmentInfo');
});
},
writeExecutorInfo(arg) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield client.writeFile(`${allureResults}/executor.json`, JSON.stringify(arg.info));
}
catch (err) {
(0, common_1.logWithPackage)('error', `Could not write executor info ${err.message}`);
}
});
},
writeCategoriesDefinitions(arg) {
return __awaiter(this, void 0, void 0, function* () {
try {
const getCategoriesContent = () => __awaiter(this, void 0, void 0, function* () {
if (typeof arg.categories !== 'string') {
return JSON.stringify(arg.categories, null, ' ');
}
const file = arg.categories;
const exists = yield client.exists(file);
if (!exists) {
(0, common_1.logWithPackage)('error', `Categories file doesn't exist '${file}'`);
return undefined;
}
const content = yield client.readFile(file);
return content.toString();
});
const contents = yield getCategoriesContent();
if (!contents) {
return;
}
yield client.writeFile(`${allureResults}/categories.json`, contents);
}
catch (err) {
(0, common_1.logWithPackage)('error', 'Could not write categories definitions info');
}
});
},
delete(arg) {
return __awaiter(this, void 0, void 0, function* () {
try {
const exists = yield client.exists(arg.path);
if (exists) {
yield client.removeFile(arg.path);
}
}
catch (err) {
log(`Could not delete: ${err.message}`);
}
});
},
deleteResults(_arg) {
return __awaiter(this, void 0, void 0, function* () {
allureReporter = new allure_reporter_plugin_1.AllureReporter(opts, taskManager, client);
try {
const exists = yield client.exists(allureResults);
if (exists) {
yield client.removeFile(allureResults);
}
}
catch (err) {
log(`Could not delete: ${err.message}`);
}
});
},
testResult(arg) {
var _a;
log(`testResult ${JSON.stringify(arg)}`);
if (allureReporter.currentTest) {
allureReporter.endAllSteps({ status: arg.result, details: arg.details });
allureReporter.currentTest.result.status = arg.result;
if ((_a = arg.details) === null || _a === void 0 ? void 0 : _a.message) {
allureReporter.currentTest.result.statusDetails.message = arg.details.message;
}
if (allureReporter.currentTestAll) {
allureReporter.currentTestAll.mochaId = arg.id;
}
}
log('testResult');
},
testEnded: (arg) => {
log(`testEnded ${JSON.stringify(arg)}`);
allureReporter.endTest(arg);
log('testEnded');
},
label: (arg) => {
log(`label ${JSON.stringify(arg)}`);
allureReporter.label(arg);
log('label');
},
suite: (arg) => {
log(`suite ${JSON.stringify(arg)}`);
allureReporter.suite(arg);
log('suite');
},
subSuite: (arg) => {
log(`subSuite ${JSON.stringify(arg)}`);
allureReporter.subSuite(arg);
log('subSuite');
},
parentSuite: (arg) => {
log(`parentSuite ${JSON.stringify(arg)}`);
allureReporter.parentSuite(arg);
log('parentSuite');
},
parameter: (arg) => {
log(`parameter ${JSON.stringify(arg)}`);
allureReporter.parameter(arg);
log('parameter');
},
testStatus: (arg) => {
log(`testStatus ${JSON.stringify(arg)}`);
allureReporter.testStatus(arg);
log('testStatus');
},
testDetails: (arg) => {
log(`testDetails ${JSON.stringify(arg)}`);
allureReporter.testDetails(arg);
log('testDetails');
},
testAttachment: (arg) => {
log(`testAttachment ${JSON.stringify(arg)}`);
allureReporter.testAttachment(arg);
log('testAttachment');
},
testFileAttachment: (arg) => {
log(`testFileAttachment ${JSON.stringify(arg)}`);
allureReporter.testFileAttachment(arg);
log('testFileAttachment');
},
fileAttachment: (arg) => {
log(`fileAttachment ${JSON.stringify(arg)}`);
allureReporter.fileAttachment(arg);
log('fileAttachment');
},
attachment: (arg) => {
log(`attachment ${JSON.stringify(arg)}`);
allureReporter.attachment(arg);
log('attachment');
},
fullName: (arg) => {
log(`fullName ${JSON.stringify(arg)}`);
allureReporter.fullName(arg);
log('fullName');
},
historyId: (arg) => {
log(`historyId ${JSON.stringify(arg)}`);
allureReporter.historyId(arg);
log('historyId');
},
link: (arg) => {
log(`link ${JSON.stringify(arg)}`);
allureReporter.link(arg);
log('link');
},
addDescriptionHtml: (arg) => {
log(`addDescriptionHtml ${JSON.stringify(arg)}`);
allureReporter.addDescriptionHtml(arg);
log('addDescriptionHtml');
},
testParameter: (arg) => {
log(`testParameter ${JSON.stringify(arg)}`);
allureReporter.testParameter(arg);
log('testParameter');
},
endAll: () => {
log('endAll started');
allureReporter.endAll();
log('endAll');
},
message: (arg) => {
log(`message ${JSON.stringify(arg)}`);
},
testMessage(arg) {
return __awaiter(this, void 0, void 0, function* () {
log(`testMessage ${JSON.stringify(arg)}`);
if (!opts.isTest) {
return;
}
try {
yield client.writeTestMessage(arg.path, arg.message);
}
catch (err) {
log(`Could not write test message: ${err.message}`);
}
});
},
screenshotOne: (arg) => {
log(`screenshotOne ${JSON.stringify(arg)}`);
allureReporter.screenshotOne(arg);
log('screenshotOne');
},
screenshotAttachment: (arg) => {
log(`screenshotAttachment ${JSON.stringify(arg)}`);
allureReporter.screenshotAttachment(arg);
log('screenshotAttachment');
},
afterSpec(arg) {
var _a;
log(`afterSpec ${JSON.stringify(arg)}`);
// Attach video via server operation
if ((_a = arg.results) === null || _a === void 0 ? void 0 : _a.video) {
const { video } = arg.results;
log(`afterSpec video path: ${video}`);
taskManager.addOperation(arg.results.spec.relative, {
type: 'allure:attachVideo',
allureResults,
videoPath: video,
allureAddVideoOnPass: opts.allureAddVideoOnPass,
});
}
else {
(0, common_1.logWithPackage)('error', 'No video path in afterSpec result');
}
// Attach screenshots via server operation
try {
const screenshots = allureReporter.getScreenshotsForSpec(arg.results);
if (screenshots.length > 0) {
taskManager.addOperation(arg.results.spec.relative, {
type: 'allure:attachScreenshots',
allureResults,
screenshots,
allTests: allureReporter.getAllTests(),
});
}
}
catch (err) {
(0, common_1.logWithPackage)('error', `Could not attach screenshots to spec: ${arg.results.spec.relative}:\n${err.message}`);
}
// Move to watch folder via server operation
if (allureResults !== allureResultsWatch) {
taskManager.addOperation(arg.results.spec.relative, {
type: 'allure:moveToWatch',
allureResults,
allureResultsWatch,
});
}
// Don't wait here - let tasks process in background
// This prevents blocking the event loop between specs
// All tasks will be awaited in after:run via waitAllFinished()
log('afterSpec - operations queued, processing in background');
},
waitAllFinished(_arg) {
return __awaiter(this, void 0, void 0, function* () {
yield taskManager.flushAllTasks();
});
},
};
};
exports.allureTasks = allureTasks;