UNPKG

@serenity-js/playwright-test

Version:

Serenity/JS test runner adapter for Playwright Test, combining Playwright's developer experience with the advanced reporting and automation capabilities of Serenity/JS

79 lines 3.12 kB
import fs from 'node:fs'; import path from 'node:path'; import { LogicError } from '@serenity-js/core'; import { CorrelationId } from '@serenity-js/core/model'; export class WorkerEventStreamWriter { outputDirectory; workerInfo; stage; beforeAllId; // = new CorrelationId('unknown'); activeSceneId; // = WorkerEventStreamWriter.beforeTest; events = new Map(); static workerStreamIdFor(workerIndex) { return new CorrelationId(`worker-${workerIndex}`); } constructor(outputDirectory, workerInfo, stage) { this.outputDirectory = outputDirectory; this.workerInfo = workerInfo; this.stage = stage; this.beforeAllId = WorkerEventStreamWriter.workerStreamIdFor(this.workerInfo.workerIndex); this.activeSceneId = this.beforeAllId; this.events.set(this.beforeAllId.value, []); } assignedTo(stage) { this.stage = stage; return this; } notifyOf(event) { if (this.isSceneEvent(event) && !this.activeSceneExistsFor(event)) { this.activateScene(event); } this.events.get(this.activeSceneId.value).push(event); } isSceneEvent(event) { return event['sceneId'] instanceof CorrelationId; } activeSceneExistsFor(event) { return this.activeSceneId.equals(event.sceneId); } activateScene(event) { this.activeSceneId = event.sceneId; const testId = event.sceneId.value; if (!this.events.has(testId)) { this.events.set(testId, []); } this.events.get(testId).push(...this.events.get(this.beforeAllId.value)); this.events.set(this.beforeAllId.value, []); } async persistAll(workerBeforeAllSceneId) { const testIds = [...this.events.keys()]; await Promise.all(testIds.map(testId => this.persist(testId, workerBeforeAllSceneId))); } async persist(testId, workerBeforeAllSceneId) { const testOutputDirectory = path.join(this.outputDirectory, testId); const filePath = path.join(testOutputDirectory, 'events.ndjson'); const events = this.flush(testId); if (events.length === 0) { return; } await fs.promises.mkdir(testOutputDirectory, { recursive: true }); for (const event of events) { const shouldReattachToScene = event['sceneId'] && event['sceneId'].equals(workerBeforeAllSceneId); const type = event.constructor.name; const value = shouldReattachToScene ? ({ ...event.toJSON(), sceneId: testId }) : event.toJSON(); const serialisedEvent = JSON.stringify({ type, value }, undefined, 0); await fs.promises.appendFile(filePath, serialisedEvent + '\n'); } } flush(testId) { if (!this.events.has(testId)) { throw new LogicError(`No events recorded for test with id ${testId}`); } const events = this.events.get(testId); this.events.set(testId, []); return events; } } //# sourceMappingURL=WorkerEventStreamWriter.js.map