Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enchance global labels definition #1221

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/allure-cucumberjs/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,59 @@ it("should handle global labels", async () => {
value: "bar",
});
});

it("should handle global labels as map", async () => {
const { tests } = await runCucumberInlineTest(
["examples"],
["examples"],
undefined,
(reporterFilePath) => `
module.exports = {
default: {
paths: ["./**/*.feature"],
format: ["summary", "${reporterFilePath}"],
formatOptions: {
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
}
}
}
}
`,
);

expect(tests).toHaveLength(2);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
expect(tests[1].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
51 changes: 51 additions & 0 deletions packages/allure-cypress/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,54 @@ it("should handle global labels", async () => {
value: "bar",
});
});

it("should handle global labels as map", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": () => `
it("passed", () => {
cy.wrap(1).should("eq", 1);
});
`,
"cypress.config.js": ({ allureCypressReporterModulePath, supportFilePath, specPattern, allureDirPath }) => `
const { allureCypress } = require("${allureCypressReporterModulePath}");

module.exports = {
e2e: {
baseUrl: "https://allurereport.org",
supportFile: "${supportFilePath}",
specPattern: "${specPattern}",
viewportWidth: 1240,
setupNodeEvents: (on, config) => {
allureCypress(on, config, {
resultsDir: "${allureDirPath}",
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
}
});

return config;
},
},
};
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
41 changes: 41 additions & 0 deletions packages/allure-jasmine/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,44 @@ it("should handle global labels", async () => {
value: "bar",
});
});

it("should handle global labels as map", async () => {
const { tests } = await runJasmineInlineTest({
"spec/sample.spec.js": `
it("should pass 1", () => {
expect(true).toBe(true);
});
`,
"spec/helpers/allure.js": `
const AllureJasmineReporter = require("allure-jasmine");

const reporter = new AllureJasmineReporter({
testMode: true,
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
}
});

jasmine.getEnv().addReporter(reporter);
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
42 changes: 42 additions & 0 deletions packages/allure-jest/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,45 @@ it("should handle global labels", async () => {
value: "bar",
});
});

it("should handle global labels as map", async () => {
const { tests } = await runJestInlineTest({
"sample.spec.js": `
it("should pass", () => {
expect(true).toBe(true);
});
`,
"jest.config.js": ({ allureJestNodePath }) => `
const config = {
bail: false,
testEnvironment: "${allureJestNodePath}",
testEnvironmentOptions: {
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
}
},
};

module.exports = config;
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
25 changes: 23 additions & 2 deletions packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,34 @@ export class ReporterRuntime {
linkConfig?: LinkConfig;
globalLabels: Label[] = [];

constructor({ writer, listeners = [], environmentInfo, categories, links, globalLabels }: ReporterRuntimeConfig) {
constructor({
writer,
listeners = [],
environmentInfo,
categories,
links,
globalLabels = {},
}: ReporterRuntimeConfig) {
this.writer = resolveWriter(writer);
this.notifier = new Notifier({ listeners });
this.categories = categories;
this.environmentInfo = environmentInfo;
this.linkConfig = links;
this.globalLabels = globalLabels ?? [];

if (Array.isArray(globalLabels)) {
this.globalLabels = globalLabels;
} else if (Object.keys(globalLabels).length) {
this.globalLabels = Object.entries(globalLabels).flatMap(([name, value]) => {
if (Array.isArray(value)) {
return value.map((v) => ({ name, value: v }));
}

return {
name,
value,
};
});
}
}

startScope = (): string => {
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
Expand Up @@ -47,10 +47,12 @@ export type LinkConfig<TOpts extends LinkTypeOptions = LinkTypeOptions> = Partia

export type WriterDescriptor = [cls: string, ...args: readonly unknown[]] | string;

export type GlobalLabelsConfig = Record<string, string | string[]>;

export interface ReporterConfig {
readonly resultsDir?: string;
readonly links?: LinkConfig;
readonly globalLabels?: Label[];
readonly globalLabels?: Label[] | GlobalLabelsConfig;
readonly listeners?: LifecycleListener[];
readonly environmentInfo?: EnvironmentInfo;
readonly categories?: Category[];
Expand Down
36 changes: 32 additions & 4 deletions packages/allure-mocha/test/spec/framework/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeAll, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import type { AllureResults } from "allure-js-commons/sdk";
import { runMochaInlineTest } from "../../utils.js";

describe("global labels", () => {
let results: AllureResults;

beforeAll(async () => {
it("should handle global labels", async () => {
results = await runMochaInlineTest(
{
globalLabels: [
Expand All @@ -17,13 +17,41 @@ describe("global labels", () => {
},
["plain-mocha", "testInSuite"],
);
});

it("should handle global labels", () => {
expect(results.tests).toHaveLength(1);
expect(results.tests[0].labels[0]).toEqual({
name: "foo",
value: "bar",
});
});

it("should handle global labels as map", async () => {
results = await runMochaInlineTest(
{
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
},
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toHaveLength(1);
expect(results.tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
});
4 changes: 2 additions & 2 deletions packages/allure-mocha/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import * as path from "node:path";
import { type Label, Status, attachment, attachmentPath, logStep, parameter, step } from "allure-js-commons";
import type { AllureResults, Category } from "allure-js-commons/sdk";
import { MessageReader, getPosixPath } from "allure-js-commons/sdk/reporter";
import { type GlobalLabelsConfig, MessageReader, getPosixPath } from "allure-js-commons/sdk/reporter";
import type { AllureMochaReporterConfig } from "../src/types.js";

type MochaRunOptions = {
Expand All @@ -15,7 +15,7 @@ type MochaRunOptions = {
extraReporters?: AllureMochaReporterConfig["extraReporters"];
inputFiles?: string[];
outputFiles?: Record<string, string>;
globalLabels?: Label[];
globalLabels?: Label[] | GlobalLabelsConfig;
};

type TestPlanEntryFixture = {
Expand Down
28 changes: 19 additions & 9 deletions packages/allure-playwright/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ it("should handle global labels", async () => {
require.resolve("allure-playwright"),
{
resultsDir: "./allure-results",
globalLabels: [
{
name: "foo",
value: "bar"
globalLabels: {
foo: "bar",
bar: ["beep", "boop"],
}
]
},
],
["dot"],
Expand All @@ -36,8 +34,20 @@ it("should handle global labels", async () => {
});

expect(tests).toHaveLength(1);
expect(tests[0].labels[0]).toEqual({
name: "foo",
value: "bar",
});
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{
name: "foo",
value: "bar",
},
{
name: "bar",
value: "beep",
},
{
name: "bar",
value: "boop",
},
]),
);
});
Loading
Loading