diff --git a/packages/allure-codeceptjs/src/reporter.ts b/packages/allure-codeceptjs/src/reporter.ts index 8beb27f32..474bca1c4 100644 --- a/packages/allure-codeceptjs/src/reporter.ts +++ b/packages/allure-codeceptjs/src/reporter.ts @@ -1,15 +1,8 @@ import { event } from "codeceptjs"; import path from "node:path"; -import { env } from "node:process"; import { LabelName, Stage, Status, type StepResult } from "allure-js-commons"; import { type RuntimeMessage, extractMetadataFromString, getMessageAndTraceFromError } from "allure-js-commons/sdk"; -import { - FileSystemWriter, - MessageWriter, - ReporterRuntime, - getEnvironmentLabels, - md5, -} from "allure-js-commons/sdk/reporter"; +import { ReporterRuntime, createDefaultWriter, getEnvironmentLabels, md5 } from "allure-js-commons/sdk/reporter"; import type { ReporterConfig } from "allure-js-commons/sdk/reporter"; import { extractMeta } from "./helpers.js"; import type { CodeceptError, CodeceptHook, CodeceptStep, CodeceptTest } from "./model.js"; @@ -22,16 +15,12 @@ export class AllureCodeceptJsReporter { currentTest: CodeceptTest | null = null; config!: ReporterConfig; - constructor(config: ReporterConfig) { + constructor(config: ReporterConfig = {}) { this.registerEvents(); - this.config = config || ({} as ReporterConfig); + this.config = config; this.allureRuntime = new ReporterRuntime({ ...config, - writer: env.ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir: config.resultsDir || "./allure-results", - }), + writer: createDefaultWriter(config), }); } diff --git a/packages/allure-cucumberjs/src/reporter.ts b/packages/allure-cucumberjs/src/reporter.ts index f814fd9ae..f302e31ec 100644 --- a/packages/allure-cucumberjs/src/reporter.ts +++ b/packages/allure-cucumberjs/src/reporter.ts @@ -16,10 +16,9 @@ import { ContentType, LabelName, Stage, Status } from "allure-js-commons"; import { getMessageAndTraceFromError } from "allure-js-commons/sdk"; import { ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE, - FileSystemWriter, - MessageWriter, ReporterRuntime, applyLinkTemplate, + createDefaultWriter, createStepResult, getEnvironmentLabels, getWorstStepResultStatus, @@ -28,7 +27,7 @@ import { import { AllureCucumberWorld } from "./legacy.js"; import type { AllureCucumberLinkConfig, AllureCucumberReporterConfig, LabelConfig } from "./model.js"; -const { ALLURE_THREAD_NAME, ALLURE_TEST_MODE } = process.env; +const { ALLURE_THREAD_NAME } = process.env; export default class AllureCucumberReporter extends Formatter { private readonly afterHooks: Record = {}; @@ -53,19 +52,10 @@ export default class AllureCucumberReporter extends Formatter { constructor(options: IFormatterOptions) { super(options); - const { - resultsDir = "./allure-results", - links, - labels, - ...rest - } = options.parsedArgvOptions as AllureCucumberReporterConfig; + const { resultsDir, links, labels, ...rest } = options.parsedArgvOptions as AllureCucumberReporterConfig; this.allureRuntime = new ReporterRuntime({ - writer: ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir, - }), + writer: createDefaultWriter({ resultsDir }), links, ...rest, }); diff --git a/packages/allure-cypress/src/reporter.ts b/packages/allure-cypress/src/reporter.ts index 51cdd6035..85eda95e9 100644 --- a/packages/allure-cypress/src/reporter.ts +++ b/packages/allure-cypress/src/reporter.ts @@ -3,8 +3,8 @@ import { ContentType, LabelName, Stage, Status } from "allure-js-commons"; import type { RuntimeMessage } from "allure-js-commons/sdk"; import { extractMetadataFromString } from "allure-js-commons/sdk"; import { - FileSystemWriter, ReporterRuntime, + createDefaultWriter, getEnvironmentLabels, getSuiteLabels, parseTestPlan, @@ -25,14 +25,12 @@ export class AllureCypress { globalHooksMessages: CypressMessage[] = []; videoOnFailOnly: boolean = false; - constructor(config?: AllureCypressConfig) { - const { resultsDir = "./allure-results", videoOnFailOnly = false, ...rest } = config || {}; + constructor(config: AllureCypressConfig = {}) { + const { resultsDir, videoOnFailOnly = false, ...rest } = config; this.videoOnFailOnly = videoOnFailOnly; this.allureRuntime = new ReporterRuntime({ - writer: new FileSystemWriter({ - resultsDir, - }), + writer: createDefaultWriter({ resultsDir }), ...rest, }); } diff --git a/packages/allure-jasmine/src/index.ts b/packages/allure-jasmine/src/index.ts index 14c92099b..3ff8ba9e2 100644 --- a/packages/allure-jasmine/src/index.ts +++ b/packages/allure-jasmine/src/index.ts @@ -1,13 +1,11 @@ -import { env } from "node:process"; import * as allure from "allure-js-commons"; import { Stage, Status } from "allure-js-commons"; import type { RuntimeMessage } from "allure-js-commons/sdk"; import { getMessageAndTraceFromError, getStatusFromError, isPromise } from "allure-js-commons/sdk"; import type { FixtureType, ReporterConfig } from "allure-js-commons/sdk/reporter"; import { - FileSystemWriter, - MessageWriter, ReporterRuntime, + createDefaultWriter, getEnvironmentLabels, getSuiteLabels, hasSkipLabel, @@ -28,8 +26,6 @@ class AllureJasmineTestRuntime extends MessageTestRuntime { } } -const { ALLURE_TEST_MODE } = env; - export default class AllureJasmineReporter implements jasmine.CustomReporter { private readonly allureRuntime: ReporterRuntime; private currentAllureTestUuid?: string; @@ -37,16 +33,12 @@ export default class AllureJasmineReporter implements jasmine.CustomReporter { private jasmineSuitesStack: jasmine.SuiteResult[] = []; private scopesStack: string[] = []; - constructor(config: ReporterConfig) { - const { resultsDir = "./allure-results", ...restConfig } = config || {}; + constructor(config: ReporterConfig = {}) { + const { resultsDir, ...restConfig } = config; this.allureRuntime = new ReporterRuntime({ ...restConfig, - writer: ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir, - }), + writer: createDefaultWriter({ resultsDir }), }); const testRuntime = new AllureJasmineTestRuntime(this); diff --git a/packages/allure-jest/src/environmentFactory.ts b/packages/allure-jest/src/environmentFactory.ts index b9d6d2c54..f336f13ff 100644 --- a/packages/allure-jest/src/environmentFactory.ts +++ b/packages/allure-jest/src/environmentFactory.ts @@ -9,9 +9,8 @@ import type { RuntimeMessage } from "allure-js-commons/sdk"; import { getMessageAndTraceFromError, getStatusFromError } from "allure-js-commons/sdk"; import type { TestPlanV1 } from "allure-js-commons/sdk"; import { - FileSystemWriter, - MessageWriter, ReporterRuntime, + createDefaultWriter, getEnvironmentLabels, getSuiteLabels, parseTestPlan, @@ -21,7 +20,7 @@ import { AllureJestTestRuntime } from "./AllureJestTestRuntime.js"; import type { AllureJestConfig, AllureJestEnvironment, AllureJestProjectConfig, RunContext } from "./model.js"; import { getTestId, getTestPath, isTestPresentInTestPlan, last, shouldHookBeSkipped } from "./utils.js"; -const { ALLURE_TEST_MODE, ALLURE_HOST_NAME, ALLURE_THREAD_NAME, JEST_WORKER_ID } = process.env; +const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME, JEST_WORKER_ID } = process.env; const hostname = os.hostname(); const createJestEnvironment = (Base: T): T => { @@ -43,15 +42,11 @@ const createJestEnvironment = (Base: T): T => super(config as JestEnvironmentConfig, context); const projectConfig = "projectConfig" in config ? config.projectConfig : config; - const { resultsDir = "allure-results", ...restConfig } = projectConfig?.testEnvironmentOptions || {}; + const { resultsDir, ...restConfig } = projectConfig?.testEnvironmentOptions || {}; this.runtime = new ReporterRuntime({ ...restConfig, - writer: ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir, - }), + writer: createDefaultWriter({ resultsDir }), }); this.testPath = context.testPath.replace(projectConfig.rootDir, "").replace(sep, ""); this.testPlan = parseTestPlan(); diff --git a/packages/allure-js-commons/src/sdk/reporter/utils.ts b/packages/allure-js-commons/src/sdk/reporter/utils.ts index ebe15257a..4566be727 100644 --- a/packages/allure-js-commons/src/sdk/reporter/utils.ts +++ b/packages/allure-js-commons/src/sdk/reporter/utils.ts @@ -1,5 +1,6 @@ import { readFile } from "fs/promises"; import { createHash, randomUUID } from "node:crypto"; +import type { EventEmitter } from "node:events"; import fs from "node:fs"; import path from "node:path"; import process from "node:process"; @@ -7,6 +8,8 @@ import properties from "properties"; import type { Label, Link, Status, StepResult, TestResult } from "../../model.js"; import { LabelName, LinkType, StatusByPriority } from "../../model.js"; import type { LinkConfig, LinkTemplate } from "./types.js"; +import { FileSystemWriter } from "./writer/FileSystemWriter.js"; +import { MessageWriter } from "./writer/MessageWriter.js"; export const randomUuid = () => { return randomUUID(); @@ -243,3 +246,11 @@ export const formatLink = (templates: LinkConfig, link: Link) => { export const formatLinks = (templates: LinkConfig, links: readonly Link[]) => links.map((link) => formatLink(templates, link)); + +export const createDefaultWriter = (config: { resultsDir?: string; emitter?: EventEmitter }) => { + return process.env.ALLURE_TEST_MODE + ? new MessageWriter(config.emitter) + : new FileSystemWriter({ + resultsDir: config.resultsDir || "./allure-results", + }); +}; diff --git a/packages/allure-mocha/src/AllureMochaReporter.ts b/packages/allure-mocha/src/AllureMochaReporter.ts index fc10bff79..da4060f99 100644 --- a/packages/allure-mocha/src/AllureMochaReporter.ts +++ b/packages/allure-mocha/src/AllureMochaReporter.ts @@ -5,8 +5,8 @@ import type { Category, RuntimeMessage } from "allure-js-commons/sdk"; import { getStatusFromError } from "allure-js-commons/sdk"; import type { ReporterConfig } from "allure-js-commons/sdk/reporter"; import { - FileSystemWriter, ReporterRuntime, + createDefaultWriter, ensureSuiteLabels, getEnvironmentLabels, getPackageLabelFromPath, @@ -55,11 +55,11 @@ export class AllureMochaReporter extends Mocha.reporters.Base { constructor(runner: Mocha.Runner, opts: Mocha.MochaOptions, isInWorker: boolean = false) { super(runner, opts); - const { resultsDir = "allure-results", ...restOptions }: ReporterConfig = opts.reporterOptions || {}; + const { resultsDir, ...restOptions }: ReporterConfig = opts.reporterOptions || {}; this.isInWorker = isInWorker; this.runtime = new ReporterRuntime({ - writer: new FileSystemWriter({ resultsDir }), + writer: createDefaultWriter({ resultsDir }), ...restOptions, }); this.testplan = createTestPlanIndices(); diff --git a/packages/allure-playwright/src/index.ts b/packages/allure-playwright/src/index.ts index 1ecaae23d..169366cbc 100644 --- a/packages/allure-playwright/src/index.ts +++ b/packages/allure-playwright/src/index.ts @@ -24,9 +24,8 @@ import type { RuntimeMessage, TestPlanV1Test } from "allure-js-commons/sdk"; import { extractMetadataFromString, getMessageAndTraceFromError, hasLabel, stripAnsi } from "allure-js-commons/sdk"; import { ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE, - FileSystemWriter, - MessageWriter, ReporterRuntime, + createDefaultWriter, escapeRegExp, getEnvironmentLabels, md5, @@ -152,16 +151,10 @@ export class AllureReporter implements ReporterV2 { onStdOut(): void {} onBegin(suite: Suite): void { - const writer = process.env.ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir: this.options.resultsDir || "./allure-results", - }); - this.suite = suite; this.allureRuntime = new ReporterRuntime({ ...this.options, - writer, + writer: createDefaultWriter({ resultsDir: this.options.resultsDir }), }); } diff --git a/packages/allure-vitest/src/reporter.ts b/packages/allure-vitest/src/reporter.ts index 06cfb8d7d..03241586c 100644 --- a/packages/allure-vitest/src/reporter.ts +++ b/packages/allure-vitest/src/reporter.ts @@ -1,14 +1,13 @@ import { normalize, relative } from "node:path"; -import { cwd, env } from "node:process"; +import { cwd } from "node:process"; import type { File, Reporter, Task } from "vitest"; import { LabelName, Stage, Status } from "allure-js-commons"; import type { RuntimeMessage } from "allure-js-commons/sdk"; import { extractMetadataFromString } from "allure-js-commons/sdk"; import type { ReporterConfig } from "allure-js-commons/sdk/reporter"; import { - FileSystemWriter, - MessageWriter, ReporterRuntime, + createDefaultWriter, getEnvironmentLabels, getHostLabel, getSuiteLabels, @@ -25,16 +24,10 @@ export default class AllureVitestReporter implements Reporter { } onInit() { - const { listeners, ...config } = this.config; - const writer = env.ALLURE_TEST_MODE - ? new MessageWriter() - : new FileSystemWriter({ - resultsDir: config.resultsDir || "./allure-results", - }); - + const { listeners, resultsDir, ...config } = this.config; this.allureReporterRuntime = new ReporterRuntime({ ...config, - writer, + writer: createDefaultWriter({ resultsDir }), listeners, }); } diff --git a/packages/newman-reporter-allure/src/index.ts b/packages/newman-reporter-allure/src/index.ts index 5cae2b5c7..fc8c0e666 100644 --- a/packages/newman-reporter-allure/src/index.ts +++ b/packages/newman-reporter-allure/src/index.ts @@ -1,14 +1,12 @@ /* eslint-disable @typescript-eslint/unbound-method */ import type { EventEmitter } from "events"; import type { ConsoleEvent, Cursor, NewmanRunExecutionAssertion } from "newman"; -import { env } from "node:process"; import type { CollectionDefinition, Event, HeaderList, Item, Request, Response } from "postman-collection"; import { ContentType, LabelName, Stage, Status } from "allure-js-commons"; import type { ReporterConfig } from "allure-js-commons/sdk/reporter"; import { - FileSystemWriter, - MessageWriter, ReporterRuntime, + createDefaultWriter, getEnvironmentLabels, getSuiteLabels, } from "allure-js-commons/sdk/reporter"; @@ -32,18 +30,14 @@ class AllureReporter { collection: CollectionDefinition; }, ) { - const { resultsDir = "./allure-results", ...restConfig } = reporterConfig; + const { resultsDir, ...restConfig } = reporterConfig; this.currentCollection = options.collection; this.rootCollectionName = options.collection.name; this.allureConfig = reporterConfig; this.allureRuntime = new ReporterRuntime({ ...restConfig, - writer: env.ALLURE_TEST_MODE - ? new MessageWriter(emitter) - : new FileSystemWriter({ - resultsDir, - }), + writer: createDefaultWriter({ resultsDir, emitter }), }); this.registerEvents(emitter); }