From a88b2e5d58cc4d3cc9083b86e16a683344b1900c Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Thu, 29 Aug 2024 12:50:12 +0100 Subject: [PATCH] fix(allure-cucumberjs): fix timings (fixes #1124, via #1125) --- packages/allure-cucumberjs/src/reporter.ts | 17 ++++--- .../test/samples/features/duration.feature | 4 ++ .../test/samples/support/duration.cjs | 14 ++++++ .../test/spec/duration.test.ts | 44 +++++++++++++++++++ 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 packages/allure-cucumberjs/test/samples/features/duration.feature create mode 100644 packages/allure-cucumberjs/test/samples/support/duration.cjs create mode 100644 packages/allure-cucumberjs/test/spec/duration.test.ts diff --git a/packages/allure-cucumberjs/src/reporter.ts b/packages/allure-cucumberjs/src/reporter.ts index 46169f38e..45c5feed2 100644 --- a/packages/allure-cucumberjs/src/reporter.ts +++ b/packages/allure-cucumberjs/src/reporter.ts @@ -1,6 +1,7 @@ import type { IFormatterOptions, TestCaseHookDefinition } from "@cucumber/cucumber"; import { Formatter, World } from "@cucumber/cucumber"; import type * as messages from "@cucumber/messages"; +import { TimeConversion } from "@cucumber/messages"; import { AttachmentContentEncoding, type PickleTag, @@ -243,7 +244,7 @@ export default class AllureCucumberReporter extends Formatter { labels: [], links: [], testCaseId: md5(fullName), - start: data.timestamp.nanos / 1000, + start: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp), fullName, }; @@ -318,7 +319,7 @@ export default class AllureCucumberReporter extends Formatter { }; } }); - this.allureRuntime.stopTest(testUuid, { stop: data.timestamp.nanos / 1000 }); + this.allureRuntime.stopTest(testUuid, { stop: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp) }); this.allureRuntime.writeTest(testUuid); this.testResultUuids.delete(data.testCaseStartedId); @@ -353,7 +354,7 @@ export default class AllureCucumberReporter extends Formatter { const fixtureUuid = this.allureRuntime.startFixture(scopeUuid, type, { name, stage: Stage.RUNNING, - start: data.timestamp.nanos / 1000, + start: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp), }); if (fixtureUuid) { this.fixtureUuids.set(data.testCaseStartedId, fixtureUuid); @@ -380,7 +381,7 @@ export default class AllureCucumberReporter extends Formatter { const stepUuid = this.allureRuntime.startStep(testUuid, undefined, { ...createStepResult(), name: `${stepKeyword}${stepPickle.text}`, - start: data.timestamp.nanos / 1000, + start: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp), }); if (!stepPickle.argument?.dataTable) { @@ -424,7 +425,9 @@ export default class AllureCucumberReporter extends Formatter { }); } }); - this.allureRuntime.stopFixture(fixtureUuid, { stop: data.timestamp.nanos / 1000 }); + this.allureRuntime.stopFixture(fixtureUuid, { + stop: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp), + }); this.fixtureUuids.delete(data.testCaseStartedId); return; } @@ -455,7 +458,9 @@ export default class AllureCucumberReporter extends Formatter { } }); - this.allureRuntime.stopStep(currentStep, { stop: data.timestamp.nanos / 1000 }); + this.allureRuntime.stopStep(currentStep, { + stop: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp), + }); } private onAttachment(message: messages.Attachment): void { diff --git a/packages/allure-cucumberjs/test/samples/features/duration.feature b/packages/allure-cucumberjs/test/samples/features/duration.feature new file mode 100644 index 000000000..35bb301d9 --- /dev/null +++ b/packages/allure-cucumberjs/test/samples/features/duration.feature @@ -0,0 +1,4 @@ +Feature: duration + + Scenario: scenario with sleep + Given a sleep diff --git a/packages/allure-cucumberjs/test/samples/support/duration.cjs b/packages/allure-cucumberjs/test/samples/support/duration.cjs new file mode 100644 index 000000000..832cb3b1e --- /dev/null +++ b/packages/allure-cucumberjs/test/samples/support/duration.cjs @@ -0,0 +1,14 @@ +const { Given, Before } = require("@cucumber/cucumber"); + +const sleep = (ms) => + new Promise((resolve) => { + setTimeout(resolve, ms); + }); + +Before(async () => { + await sleep(80); +}); + +Given("a sleep", async () => { + await sleep(100); +}); diff --git a/packages/allure-cucumberjs/test/spec/duration.test.ts b/packages/allure-cucumberjs/test/spec/duration.test.ts new file mode 100644 index 000000000..fd440ff20 --- /dev/null +++ b/packages/allure-cucumberjs/test/spec/duration.test.ts @@ -0,0 +1,44 @@ +import { expect, it } from "vitest"; +import { runCucumberInlineTest } from "../utils.js"; + +it("should set correct timings for tests", async () => { + const before = new Date().getTime(); + const { tests } = await runCucumberInlineTest(["duration"], ["duration"]); + const after = new Date().getTime(); + + expect(tests).toHaveLength(1); + const [tr] = tests; + expect(tr.name).toEqual("scenario with sleep"); + expect(tr.start).toBeGreaterThanOrEqual(before); + expect(tr.start).toBeLessThanOrEqual(after); + expect(tr.stop).toBeGreaterThanOrEqual(tr.start!); +}); + +it("should set correct timings for steps", async () => { + const before = new Date().getTime(); + const { tests } = await runCucumberInlineTest(["duration"], ["duration"]); + const after = new Date().getTime(); + + expect(tests).toHaveLength(1); + const [tr] = tests; + const [s1] = tr.steps; + expect(s1.name).toEqual("Given a sleep"); + expect(s1.start).toBeGreaterThanOrEqual(before); + expect(s1.start).toBeLessThanOrEqual(after); + expect(s1.stop).toBeGreaterThanOrEqual(s1.start!); + expect(s1.stop! - s1.start!).toBeGreaterThanOrEqual(100); +}); + +it("should set correct timings for hooks", async () => { + const before = new Date().getTime(); + const { groups } = await runCucumberInlineTest(["duration"], ["duration"]); + const after = new Date().getTime(); + + expect(groups).toHaveLength(1); + const [trc] = groups; + const [f1] = trc.befores; + expect(f1.start).toBeGreaterThanOrEqual(before); + expect(f1.start).toBeLessThanOrEqual(after); + expect(f1.stop).toBeGreaterThanOrEqual(f1.start!); + expect(f1.stop! - f1.start!).toBeGreaterThanOrEqual(80); +});