From 5bbae37a4b434699946fcacf40a9599c35399737 Mon Sep 17 00:00:00 2001 From: epszaw Date: Thu, 16 Jan 2025 12:13:12 +0100 Subject: [PATCH] enchance global labels definition (via #1221) --- .../test/spec/globalLabels.test.ts | 56 +++++++++++++++++++ .../test/spec/globalLabels.test.ts | 51 +++++++++++++++++ .../test/spec/globalLabels.test.ts | 41 ++++++++++++++ .../test/spec/globalLabels.test.ts | 42 ++++++++++++++ .../src/sdk/reporter/ReporterRuntime.ts | 25 ++++++++- .../src/sdk/reporter/types.ts | 5 +- .../test/spec/framework/globalLabels.test.ts | 36 ++++++++++-- packages/allure-mocha/test/utils.ts | 4 +- .../test/spec/globalLabels.test.ts | 28 +++++++--- .../test/spec/globalLabels.test.ts | 28 +++++++--- 10 files changed, 289 insertions(+), 27 deletions(-) diff --git a/packages/allure-cucumberjs/test/spec/globalLabels.test.ts b/packages/allure-cucumberjs/test/spec/globalLabels.test.ts index b5e73dc7d..9528ccf66 100644 --- a/packages/allure-cucumberjs/test/spec/globalLabels.test.ts +++ b/packages/allure-cucumberjs/test/spec/globalLabels.test.ts @@ -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", + }, + ]), + ); +}); diff --git a/packages/allure-cypress/test/spec/globalLabels.test.ts b/packages/allure-cypress/test/spec/globalLabels.test.ts index 61b5c8fe8..055ca45fe 100644 --- a/packages/allure-cypress/test/spec/globalLabels.test.ts +++ b/packages/allure-cypress/test/spec/globalLabels.test.ts @@ -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", + }, + ]), + ); +}); diff --git a/packages/allure-jasmine/test/spec/globalLabels.test.ts b/packages/allure-jasmine/test/spec/globalLabels.test.ts index 0fea483aa..58e26df9d 100644 --- a/packages/allure-jasmine/test/spec/globalLabels.test.ts +++ b/packages/allure-jasmine/test/spec/globalLabels.test.ts @@ -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", + }, + ]), + ); +}); diff --git a/packages/allure-jest/test/spec/globalLabels.test.ts b/packages/allure-jest/test/spec/globalLabels.test.ts index c4ba462ff..225a5e17d 100644 --- a/packages/allure-jest/test/spec/globalLabels.test.ts +++ b/packages/allure-jest/test/spec/globalLabels.test.ts @@ -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", + }, + ]), + ); +}); diff --git a/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts b/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts index 5b9f75d7b..79f4a7c72 100644 --- a/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts +++ b/packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts @@ -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 => { diff --git a/packages/allure-js-commons/src/sdk/reporter/types.ts b/packages/allure-js-commons/src/sdk/reporter/types.ts index 9c2a140db..9c5021858 100644 --- a/packages/allure-js-commons/src/sdk/reporter/types.ts +++ b/packages/allure-js-commons/src/sdk/reporter/types.ts @@ -1,6 +1,7 @@ import type { FixtureResult, Label, + LabelName, Link, LinkType, Parameter, @@ -47,10 +48,12 @@ export type LinkConfig = Partia export type WriterDescriptor = [cls: string, ...args: readonly unknown[]] | string; +export type GlobalLabelsConfig = Partial> & Record; + 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[]; diff --git a/packages/allure-mocha/test/spec/framework/globalLabels.test.ts b/packages/allure-mocha/test/spec/framework/globalLabels.test.ts index ed4e09823..3f6a7c322 100644 --- a/packages/allure-mocha/test/spec/framework/globalLabels.test.ts +++ b/packages/allure-mocha/test/spec/framework/globalLabels.test.ts @@ -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: [ @@ -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", + }, + ]), + ); + }); }); diff --git a/packages/allure-mocha/test/utils.ts b/packages/allure-mocha/test/utils.ts index da1f6033e..809c9a068 100644 --- a/packages/allure-mocha/test/utils.ts +++ b/packages/allure-mocha/test/utils.ts @@ -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 = { @@ -15,7 +15,7 @@ type MochaRunOptions = { extraReporters?: AllureMochaReporterConfig["extraReporters"]; inputFiles?: string[]; outputFiles?: Record; - globalLabels?: Label[]; + globalLabels?: Label[] | GlobalLabelsConfig; }; type TestPlanEntryFixture = { diff --git a/packages/allure-playwright/test/spec/globalLabels.test.ts b/packages/allure-playwright/test/spec/globalLabels.test.ts index 602e6a663..52218c889 100644 --- a/packages/allure-playwright/test/spec/globalLabels.test.ts +++ b/packages/allure-playwright/test/spec/globalLabels.test.ts @@ -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"], @@ -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", + }, + ]), + ); }); diff --git a/packages/allure-vitest/test/spec/globalLabels.test.ts b/packages/allure-vitest/test/spec/globalLabels.test.ts index 80d522dc1..2327bc610 100644 --- a/packages/allure-vitest/test/spec/globalLabels.test.ts +++ b/packages/allure-vitest/test/spec/globalLabels.test.ts @@ -23,12 +23,10 @@ describe("global labels", () => { "allure-vitest/reporter", { resultsDir: "allure-results", - globalLabels: [ - { - name: "foo", - value: "bar" + globalLabels: { + foo: "bar", + bar: ["beep", "boop"], } - ] } ], ], @@ -39,9 +37,21 @@ describe("global labels", () => { ); 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", + }, + ]), + ); }); });