UNPKG

@mmisty/cypress-allure-adapter

Version:

cypress allure adapter to generate allure results during tests execution (Allure TestOps compatible)

165 lines (164 loc) 8.04 kB
"use strict"; 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.configureAllureAdapterPlugins = void 0; const debug_1 = __importDefault(require("debug")); const fs_1 = __importDefault(require("fs")); const allure_1 = require("./allure"); const server_1 = require("./server"); const allure_task_client_1 = require("./allure-task-client"); const common_1 = require("../common"); const debug = (0, debug_1.default)('cypress-allure:plugins'); // Get plugin version from package.json const getPluginVersion = () => { try { // In published package, code is at lib/plugins/index.js, package.json is at lib/package.json // eslint-disable-next-line @typescript-eslint/no-var-requires const pkg = require('../package.json'); return pkg.version ? `v${pkg.version}` : 'unknown version'; } catch (_a) { return 'unknown version'; } }; /** * Environment variable to control the task server mode * - 'remote': All operations run in a separate process (recommended for production) * - 'local': All operations run in the same process (legacy behavior) */ const ALLURE_MODE_ENV = 'ALLURE_MODE'; /** * Get the task client mode from environment or config */ const getTaskClientMode = (config) => { // Check environment variable first const envMode = process.env[ALLURE_MODE_ENV]; if (envMode === 'local' || envMode === 'remote') { return envMode; } // Check cypress config const configMode = config.env['allureMode']; if (configMode === 'local' || configMode === 'remote') { return configMode; } // Default to 'remote' for better performance return 'remote'; }; // this runs in node const configureAllureAdapterPlugins = (on, config) => { var _a, _b; if (process.env.DEBUG) { config.env['DEBUG'] = process.env.DEBUG; } if (config.env['GREP_PRE_FILTER'] === 'true' || config.env['GREP_PRE_FILTER'] === true) { debug('Not running allure in prefiltering mode'); return undefined; } if (config.env['allure'] !== 'true' && config.env['allure'] !== true) { debug('Not running allure. Set "allure" env variable to "true" to generate allure-results'); return undefined; } debug('Register plugin'); (0, common_1.logWithPackage)('log', `${getPluginVersion()}`); const results = (_a = config.env['allureResults']) !== null && _a !== void 0 ? _a : 'allure-results'; const watchResultsPath = config.env['allureResultsWatchPath']; const allureAddVideoOnPass = config.env['allureAddVideoOnPass'] === true || config.env['allureAddVideoOnPass'] === 'true'; const showDuplicateWarn = config.env['allureShowDuplicateWarn'] ? config.env['allureShowDuplicateWarn'] === true || config.env['allureShowDuplicateWarn'] === 'true' : false; const options = { showDuplicateWarn, allureAddVideoOnPass, allureResults: results, techAllureResults: watchResultsPath !== null && watchResultsPath !== void 0 ? watchResultsPath : results, allureSkipSteps: (_b = config.env['allureSkipSteps']) !== null && _b !== void 0 ? _b : '', screenshots: config.screenshotsFolder || 'no', videos: config.videosFolder, isTest: config.env['JEST_TEST'] === 'true' || config.env['JEST_TEST'] === true, }; debug('OPTIONS:'); debug(JSON.stringify(options, null, ' ')); // Determine mode const mode = getTaskClientMode(config); debug(`Task client mode: ${mode}`); // Create the task client const client = new allure_task_client_1.AllureTaskClient(mode); // Start the task server with timeout guard (async - doesn't block Cypress startup) const SERVER_START_TIMEOUT = 20000; // 20 seconds max wait const startWithTimeout = () => __awaiter(void 0, void 0, void 0, function* () { const timeoutPromise = new Promise((_, reject) => { setTimeout(() => { reject(new Error(`Task server startup timed out after ${SERVER_START_TIMEOUT / 1000}s`)); }, SERVER_START_TIMEOUT); }); try { yield Promise.race([client.start(), timeoutPromise]); } catch (err) { (0, common_1.logWithPackage)('error', `Failed to start task server: ${err.message}`); (0, common_1.logWithPackage)('warn', 'Allure reporting may be incomplete - continuing without task server'); } }); // Fire and forget - don't block Cypress startup startWithTimeout(); // Create reporter first so we can use task manager for cleanup const reporter = (0, allure_1.allureTasks)(options, client); debug('Registered with options:'); debug(options); // Clean results if requested - done synchronously to ensure directories exist // before any watchers or other code tries to use them if (`${config.env['allureCleanResults']}` === 'true') { debug('Clean results (sync)'); try { // Remove allure results directory if (fs_1.default.existsSync(options.allureResults)) { fs_1.default.rmSync(options.allureResults, { recursive: true, force: true }); debug(`Removed directory: ${options.allureResults}`); } // Recreate allure results directory fs_1.default.mkdirSync(options.allureResults, { recursive: true }); debug(`Created directory: ${options.allureResults}`); // Remove and recreate watch directory if different if (options.techAllureResults !== options.allureResults) { if (fs_1.default.existsSync(options.techAllureResults)) { fs_1.default.rmSync(options.techAllureResults, { recursive: true, force: true }); debug(`Removed directory: ${options.techAllureResults}`); } fs_1.default.mkdirSync(options.techAllureResults, { recursive: true }); debug(`Created directory: ${options.techAllureResults}`); } (0, common_1.logWithPackage)('log', `Cleaned allure results: ${options.allureResults}`); } catch (err) { (0, common_1.logWithPackage)('error', `Failed to clean allure results: ${err.message}`); } } // Start WebSocket server async - doesn't block Cypress startup (0, server_1.startReporterServer)(config, reporter).catch(err => { (0, common_1.logWithPackage)('error', `Failed to start reporter server: ${err.message}`); }); config.reporterOptions = Object.assign(Object.assign({}, config.reporterOptions), { url: config.reporterUrl }); on('after:spec', (spec, res) => __awaiter(void 0, void 0, void 0, function* () { const results = res; reporter.afterSpec({ results }); })); on('after:run', (_results) => __awaiter(void 0, void 0, void 0, function* () { debug('after:run'); yield reporter.waitAllFinished({}); yield (0, allure_task_client_1.stopAllureTaskServer)(); (0, common_1.logWithPackage)('log', 'Processing all files finished'); })); return reporter; }; exports.configureAllureAdapterPlugins = configureAllureAdapterPlugins;