diff --git a/packages/allure-js-commons/src/sdk/reporter/LifecycleState.ts b/packages/allure-js-commons/src/sdk/reporter/LifecycleState.ts index 7d9600895..adf7ee96c 100644 --- a/packages/allure-js-commons/src/sdk/reporter/LifecycleState.ts +++ b/packages/allure-js-commons/src/sdk/reporter/LifecycleState.ts @@ -42,11 +42,12 @@ export class LifecycleState { }; // fixtures - setFixtureResult = (uuid: string, type: FixtureType, result: FixtureResult) => { + setFixtureResult = (scopeUuid: string, uuid: string, type: FixtureType, result: FixtureResult) => { const wrappedResult: FixtureWrapper = { uuid, type, value: result, + scopeUuid, }; this.fixturesResults.set(uuid, wrappedResult); return wrappedResult; @@ -59,6 +60,7 @@ export class LifecycleState { // test scopes setScope = (uuid: string, data: Partial = {}) => { const scope: TestScope = { + labels: [], fixtures: [], tests: [], ...data, diff --git a/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts b/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts index 422c58207..06adb80cb 100644 --- a/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts +++ b/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts @@ -139,7 +139,7 @@ export class ReporterRuntime { } const uuid = randomUuid(); - const wrappedFixture = this.state.setFixtureResult(uuid, type, { + const wrappedFixture = this.state.setFixtureResult(scopeUuid, uuid, type, { ...createFixtureResult(), start: Date.now(), ...fixtureResult, @@ -191,6 +191,9 @@ export class ReporterRuntime { return; } scope.tests.push(uuid); + if (scope.labels) { + testResult.labels = [...testResult.labels, ...scope.labels]; + } }); this.state.setTestResult(uuid, testResult); @@ -416,13 +419,19 @@ export class ReporterRuntime { #handleMetadataMessage = (rootUuid: string, message: RuntimeMetadataMessage["data"]) => { // only display name could be set to fixture. - const fixtureResult = this.state.getFixtureResult(rootUuid); + const fixtureResult = this.state.getWrappedFixtureResult(rootUuid); if (fixtureResult) { - this.updateFixture(rootUuid, (result) => { - if (message.displayName) { - result.name = message.displayName; - } - }); + if (message.displayName) { + this.updateFixture(rootUuid, (result) => { + result.name = message.displayName!; + }); + } + + if (message.labels) { + this.updateScope(fixtureResult.scopeUuid, (scope) => { + scope.labels = [...scope.labels, ...message.labels!]; + }); + } return; } diff --git a/packages/allure-js-commons/src/sdk/reporter/types.ts b/packages/allure-js-commons/src/sdk/reporter/types.ts index 03fc46694..02cfbf9cb 100644 --- a/packages/allure-js-commons/src/sdk/reporter/types.ts +++ b/packages/allure-js-commons/src/sdk/reporter/types.ts @@ -1,4 +1,4 @@ -import type { FixtureResult, LinkType, StepResult, TestResult, TestResultContainer } from "../../model.js"; +import type { FixtureResult, Label, LinkType, StepResult, TestResult, TestResultContainer } from "../../model.js"; import type { Category, EnvironmentInfo } from "../types.js"; export const ALLURE_METADATA_CONTENT_TYPE = "application/vnd.allure.metadata+json"; @@ -65,6 +65,7 @@ export type TestScope = { uuid: string; tests: string[]; fixtures: FixtureWrapper[]; + labels: Label[]; }; export type FixtureType = "before" | "after"; @@ -73,4 +74,5 @@ export type FixtureWrapper = { uuid: string; value: FixtureResult; type: FixtureType; + scopeUuid: string; }; diff --git a/packages/allure-js-commons/test/sdk/reporter/ReporterRuntime.spec.ts b/packages/allure-js-commons/test/sdk/reporter/ReporterRuntime.spec.ts index 7a88eb9bf..fd4d36030 100644 --- a/packages/allure-js-commons/test/sdk/reporter/ReporterRuntime.spec.ts +++ b/packages/allure-js-commons/test/sdk/reporter/ReporterRuntime.spec.ts @@ -361,6 +361,67 @@ describe("ReporterRuntime", () => { expect(writer.writeResult.mock.calls.length).toBe(0); }); + + it("should support metadata messages from before fixtures", () => { + const writer = mockWriter(); + const runtime = new ReporterRuntime({ writer }); + + const scopeUuid = runtime.startScope(); + const fixtureUuid = runtime.startFixture(scopeUuid, "before", {})!; + runtime.applyRuntimeMessages(fixtureUuid, [ + { + type: "metadata", + data: { + labels: [ + { + name: "label 1", + value: "value 1", + }, + { + name: "label 2", + value: "value 2", + }, + ], + }, + }, + ]); + runtime.stopFixture(fixtureUuid); + const testUuid = runtime.startTest({}, [scopeUuid]); + runtime.applyRuntimeMessages(testUuid, [ + { + type: "metadata", + data: { + labels: [ + { + name: "label 3", + value: "value 3", + }, + ], + }, + }, + ]); + runtime.stopTest(testUuid); + runtime.writeTest(testUuid); + runtime.writeScope(scopeUuid); + + const [testResult] = writer.writeResult.mock.calls[0]; + expect(testResult.labels).toEqual( + expect.arrayContaining([ + { + name: "label 1", + value: "value 1", + }, + { + name: "label 2", + value: "value 2", + }, + { + name: "label 3", + value: "value 3", + }, + ]), + ); + }); }); describe("load well-known writers", () => {