From 684db2b4e7b275e7326ba017944e18a5e60e783c Mon Sep 17 00:00:00 2001 From: epszaw Date: Fri, 18 Aug 2023 13:12:59 +0200 Subject: [PATCH 1/2] add better description handler for cucumber feature files --- .../src/CucumberJSAllureReporter.ts | 5 ++ .../test/specs/description_test.ts | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 packages/allure-cucumberjs/test/specs/description_test.ts diff --git a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts index 76d9551a0..fc2e51391 100644 --- a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts +++ b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts @@ -299,6 +299,10 @@ export class CucumberJSAllureFormatter extends Formatter { const currentTest = new AllureTest(this.allureRuntime, Date.now()); const thread = data.workerId || ALLURE_THREAD_NAME || process.pid.toString(); const fullName = `${pickle.uri}#${pickle.name}`; + // sometimes description starts with "Description:", but it's not actually required + const description = scenario?.description + ? scenario.description.replace(/\s+Description:\s?/, "") + : doc?.feature?.description; const testCaseId = md5(fullName); this.testCaseStartedMap.set(data.id, data); @@ -306,6 +310,7 @@ export class CucumberJSAllureFormatter extends Formatter { this.currentTestsMap.set(data.id, currentTest); currentTest.name = pickle.name; + currentTest.description = description || ""; currentTest.fullName = fullName; currentTest.testCaseId = testCaseId; diff --git a/packages/allure-cucumberjs/test/specs/description_test.ts b/packages/allure-cucumberjs/test/specs/description_test.ts new file mode 100644 index 000000000..6789fd4df --- /dev/null +++ b/packages/allure-cucumberjs/test/specs/description_test.ts @@ -0,0 +1,60 @@ +import { expect } from "chai"; +import { describe, it } from "mocha"; +import { ITestFormatterOptions, runFeatures } from "../helpers/formatter_helpers"; +import { buildSupportCodeLibrary } from "../helpers/runtime_helpers"; + +const dataSet: { [name: string]: ITestFormatterOptions } = { + featureDescription: { + supportCodeLibrary: buildSupportCodeLibrary(({ Given }) => { + Given("a step", () => {}); + }), + sources: [ + { + data: ["Feature: a", "Feature's description", " Scenario: b", " Given a step"].join("\n"), + uri: "a.feature", + }, + ], + }, + scenarioDescription: { + supportCodeLibrary: buildSupportCodeLibrary(({ Given }) => { + Given("a step", () => {}); + }), + sources: [ + { + data: [ + "Feature: a", + "Feature's description", + "", + " Scenario: b", + " Description: Scenario's description", + " Given a step", + ].join("\n"), + uri: "a.feature", + }, + ], + }, +}; + +describe("CucumberJSAllureReporter > description", () => { + describe("with feature description", () => { + it("uses feature description", async () => { + const results = await runFeatures(dataSet.featureDescription); + + expect(results.tests).length(1); + const [testResult] = results.tests; + + expect(testResult.description).eq("Feature's description"); + }); + }); + + describe("with scenario description", () => { + it("uses scenario description", async () => { + const results = await runFeatures(dataSet.scenarioDescription); + + expect(results.tests).length(1); + const [testResult] = results.tests; + + expect(testResult.description).eq("Scenario's description"); + }); + }); +}); From a81126e728708a1bb00a49a8e4aa19afef1db13c Mon Sep 17 00:00:00 2001 From: epszaw Date: Fri, 18 Aug 2023 17:03:41 +0200 Subject: [PATCH 2/2] don't modify original descriptions --- packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts | 6 ++---- packages/allure-cucumberjs/test/specs/description_test.ts | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts index fc2e51391..cfb2e1bff 100644 --- a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts +++ b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts @@ -300,9 +300,7 @@ export class CucumberJSAllureFormatter extends Formatter { const thread = data.workerId || ALLURE_THREAD_NAME || process.pid.toString(); const fullName = `${pickle.uri}#${pickle.name}`; // sometimes description starts with "Description:", but it's not actually required - const description = scenario?.description - ? scenario.description.replace(/\s+Description:\s?/, "") - : doc?.feature?.description; + const description = scenario?.description || doc?.feature?.description || ""; const testCaseId = md5(fullName); this.testCaseStartedMap.set(data.id, data); @@ -310,7 +308,7 @@ export class CucumberJSAllureFormatter extends Formatter { this.currentTestsMap.set(data.id, currentTest); currentTest.name = pickle.name; - currentTest.description = description || ""; + currentTest.description = description.trim(); currentTest.fullName = fullName; currentTest.testCaseId = testCaseId; diff --git a/packages/allure-cucumberjs/test/specs/description_test.ts b/packages/allure-cucumberjs/test/specs/description_test.ts index 6789fd4df..ab0c62816 100644 --- a/packages/allure-cucumberjs/test/specs/description_test.ts +++ b/packages/allure-cucumberjs/test/specs/description_test.ts @@ -54,7 +54,7 @@ describe("CucumberJSAllureReporter > description", () => { expect(results.tests).length(1); const [testResult] = results.tests; - expect(testResult.description).eq("Scenario's description"); + expect(testResult.description).eq("Description: Scenario's description"); }); }); });