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

fix(allure-cucumberjs): fix timings #1125

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions packages/allure-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: duration

Scenario: scenario with sleep
Given a sleep
14 changes: 14 additions & 0 deletions packages/allure-cucumberjs/test/samples/support/duration.cjs
Original file line number Diff line number Diff line change
@@ -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);
});
44 changes: 44 additions & 0 deletions packages/allure-cucumberjs/test/spec/duration.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Loading