Skip to content

Commit

Permalink
support links in string metadata for mocha and codeceptjs
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Jan 29, 2025
1 parent b1077eb commit e87421a
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 11 deletions.
10 changes: 9 additions & 1 deletion packages/allure-codeceptjs/test/spec/titleMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ it("handles title metadata", async () => {
const { tests } = await runCodeceptJsInlineTest({
"sample.test.js": `
Feature("sample feature");
Scenario("some strange name to test @allure.id=228 @allure.label.tag=slow @allure.label.labelName=labelValue", async () => {});
Scenario("some strange name to test @allure.id=228 @allure.label.tag=slow @allure.label.labelName=labelValue @allure.link.my_link=https://allurereport.org", async () => {});
`,
});

Expand All @@ -28,4 +28,12 @@ it("handles title metadata", async () => {
},
]),
);
expect(tests[0].links).toEqual(
expect.arrayContaining([
{
type: "my_link",
url: "https://allurereport.org",
},
]),
);
});
5 changes: 5 additions & 0 deletions packages/allure-mocha/src/AllureMochaReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getAllureDisplayName,
getAllureFullName,
getAllureMetaLabels,
getAllureMetaLinks,
getHookType,
getSuitesOfMochaTest,
getTestCaseId,
Expand Down Expand Up @@ -187,14 +188,17 @@ export class AllureMochaReporter extends Mocha.reporters.Base {
getThreadLabel(this.getWorkerId()),
];
const metaLabels = getAllureMetaLabels(test);
const links = getAllureMetaLinks(test);
const labels = globalLabels.concat(initialLabels, metaLabels);

if (test.file) {
const packageLabel: Label = getPackageLabel(test.file);

labels.push(packageLabel);
}

const scopeUuid = this.runtime.startScope();

setTestScope(test, scopeUuid);

// @ts-ignore
Expand All @@ -207,6 +211,7 @@ export class AllureMochaReporter extends Mocha.reporters.Base {
stage: Stage.RUNNING,
fullName: getAllureFullName(test),
labels,
links: [...links],
testCaseId: getTestCaseId(test),
parameters: parameters,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/allure-mocha/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReporterConstructor } from "mocha";
import type { Label } from "allure-js-commons";
import type { Label, Link } from "allure-js-commons";
import type { ReporterConfig } from "allure-js-commons/sdk/reporter";

export type TestPlanIndices = {
Expand All @@ -11,6 +11,7 @@ export type AllureMochaTestData = {
isIncludedInTestRun: boolean;
fullName: string;
labels: readonly Label[];
links: readonly Link[];
displayName: string;
scope?: string;
};
Expand Down
7 changes: 7 additions & 0 deletions packages/allure-mocha/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ const allureMochaDataKey = Symbol("Used to access Allure extra data in Mocha obj

const getAllureData = (item: Mocha.Test): AllureMochaTestData => {
const data = (item as any)[allureMochaDataKey];

if (!data) {
const meta = extractMetadataFromString(item.title);
const defaultData: AllureMochaTestData = {
isIncludedInTestRun: true,
fullName: createAllureFullName(item),
labels: meta.labels,
links: meta.links,
displayName: meta.cleanTitle,
};

(item as any)[allureMochaDataKey] = defaultData;

return defaultData;
}

return data;
};

Expand Down Expand Up @@ -55,6 +60,8 @@ export const isIncludedInTestRun = (test: Mocha.Test) => getAllureData(test).isI

export const getAllureMetaLabels = (test: Mocha.Test) => getAllureData(test).labels;

export const getAllureMetaLinks = (test: Mocha.Test) => getAllureData(test).links;

export const getAllureId = (data: AllureMochaTestData) => {
const values = data.labels.filter((l) => l.name === LabelName.ALLURE_ID).map((l) => l.value);
if (values.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// cjs: const { it } = require("mocha");
// esm: import { it } from "mocha";

it("a test two embedded custom link @allure.link.foo=bar @allure.link.baz:qux", async () => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// cjs: const { it } = require("mocha");
// esm: import { it } from "mocha";

it("a test with an embedded custom link @allure.link.foo:bar", async () => {});
34 changes: 34 additions & 0 deletions packages/allure-mocha/test/spec/api/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("embedded metadata", () => {
["labels", "meta", "single"],
["labels", "meta", "multiple"],
["labels", "meta", "change"],
["links", "meta", "single"],
["links", "meta", "multiple"],
));
});

Expand Down Expand Up @@ -68,4 +70,36 @@ describe("embedded metadata", () => {
expect(testCaseIdBefore).toBe(testCaseIdAfter);
expect(historyIdBefore).toBe(historyIdAfter);
});

it("may apply a custom link", () => {
expect(tests).toContainEqual(
expect.objectContaining({
name: "a test with an embedded custom link",
links: expect.arrayContaining([
{
type: "foo",
url: "bar",
},
]),
}),
);
});

it("may apply multiple links", () => {
expect(tests).toContainEqual(
expect.objectContaining({
name: "a test two embedded custom link",
links: expect.arrayContaining([
{
type: "foo",
url: "bar",
},
{
type: "baz",
url: "qux",
},
]),
}),
);
});
});
1 change: 1 addition & 0 deletions packages/allure-mocha/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export const runMochaInlineTest = async (
...samples: (string | string[])[]
) => {
let options: MochaRunOptions;

if (typeof sampleOrConfig === "object" && !(sampleOrConfig instanceof Array)) {
options = sampleOrConfig;
} else {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-playwright/test/spec/titleMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ it("has metadata from title", async () => {
{ name: LabelName.TAG, value: "slow" },
{ name: "labelName", value: "labelValue" },
]),
links: expect.arrayContaining([
{ type: "my_link", url: "https://allurereport.org" },
]),
links: expect.arrayContaining([{ type: "my_link", url: "https://allurereport.org" }]),
}),
]);
});
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-vitest/test/spec/titleMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ describe("title metadata", () => {
{ name: LabelName.ALLURE_ID, value: "1" },
{ name: "bar", value: "2" },
]),
links: expect.arrayContaining([
{ type: "my_link", url: "https://allurereport.org" },
])
links: expect.arrayContaining([{ type: "my_link", url: "https://allurereport.org" }]),
});
});
});
4 changes: 1 addition & 3 deletions packages/newman-reporter-allure/test/spec/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ test("complex test overview", async () => {
{ name: LabelName.ALLURE_ID, value: "228" },
{ name: "custom", value: "test" },
]),
links: expect.arrayContaining([
{ type: "my_link", url: "https://allurereport.org" },
]),
links: expect.arrayContaining([{ type: "my_link", url: "https://allurereport.org" }]),
parameters: expect.arrayContaining([
expect.objectContaining({ name: "Request", value: "GET - http://example.com/test?dfgdfg" }),
expect.objectContaining({ name: "Response Code", value: "200", excluded: true }),
Expand Down

0 comments on commit e87421a

Please sign in to comment.