Skip to content

Commit

Permalink
process metadata messages from before hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Jul 3, 2024
1 parent 21563f8 commit d062d52
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,6 +60,7 @@ export class LifecycleState {
// test scopes
setScope = (uuid: string, data: Partial<TestScope> = {}) => {
const scope: TestScope = {
labels: [],
fixtures: [],
tests: [],
...data,
Expand Down
23 changes: 16 additions & 7 deletions packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/allure-js-commons/src/sdk/reporter/types.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -65,6 +65,7 @@ export type TestScope = {
uuid: string;
tests: string[];
fixtures: FixtureWrapper[];
labels: Label[];
};

export type FixtureType = "before" | "after";
Expand All @@ -73,4 +74,5 @@ export type FixtureWrapper = {
uuid: string;
value: FixtureResult;
type: FixtureType;
scopeUuid: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit d062d52

Please sign in to comment.