UNPKG

@playwright-mocks/reporters

Version:

Single Playwright reporter for mock interceptor with configurable logging and HTML report generation utility

98 lines 3.64 kB
import * as fs from 'fs'; import * as path from 'path'; import { LoggingService } from '../logging.service.js'; export class TestsRunOutputService { runData; options; runId; loggingService; constructor(options) { this.options = options; this.runId = this.generateRunId(); this.loggingService = new LoggingService({ logs: true, verbose: true, generateHtml: false, htmlOutputDir: './mocks-report', htmlTitle: 'Playwright Mocks Report', runOutputDir: './test-runs', saveAttachments: true }); this.runData = { runId: this.runId, startTime: Date.now(), endTime: 0, totalTests: 0, passedTests: 0, failedTests: 0, skippedTests: 0, tests: [], metadata: { nodeVersion: process.version, platform: process.platform } }; this.loggingService.logBegin(0); // Initialize logging } generateRunId() { return `run-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; } addTest(testInfo) { this.runData.tests.push(testInfo); this.updateCounts(); this.loggingService.logTestEnd(testInfo); } updateCounts() { this.runData.totalTests = this.runData.tests.length; this.runData.passedTests = this.runData.tests.filter(t => t.status === 'passed').length; this.runData.failedTests = this.runData.tests.filter(t => t.status === 'failed').length; this.runData.skippedTests = this.runData.tests.filter(t => t.status === 'skipped').length; } finalize() { this.runData.endTime = Date.now(); this.loggingService.logSummary(this.runData.tests); this.saveRunData(); } saveRunData() { try { // Ensure output directory exists if (!fs.existsSync(this.options.outputDir)) { fs.mkdirSync(this.options.outputDir, { recursive: true }); } const runFilePath = path.join(this.options.outputDir, `${this.runId}.json`); fs.writeFileSync(runFilePath, JSON.stringify(this.runData, null, 2)); // Save attachments if enabled if (this.options.saveAttachments) { this.saveAttachments(); } } catch (error) { console.error('TestsRunOutputService: Failed to save run data:', error); } } saveAttachments() { const attachmentsDir = path.join(this.options.outputDir, `${this.runId}-attachments`); if (!fs.existsSync(attachmentsDir)) { fs.mkdirSync(attachmentsDir, { recursive: true }); } this.runData.tests.forEach((test, testIndex) => { if (test.attachments && test.attachments.length > 0) { const testAttachmentsDir = path.join(attachmentsDir, `test-${testIndex}`); fs.mkdirSync(testAttachmentsDir, { recursive: true }); test.attachments.forEach((attachment, attachmentIndex) => { const attachmentPath = path.join(testAttachmentsDir, `${attachmentIndex}-${attachment.name || 'attachment'}`); if (attachment.body) { fs.writeFileSync(attachmentPath, attachment.body); } }); } }); } getRunData() { return this.runData; } getRunId() { return this.runId; } } //# sourceMappingURL=tests-run-output.service.js.map