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(vitest): fix test duration and hostname #881

Merged
merged 2 commits into from
Feb 15, 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
9 changes: 5 additions & 4 deletions packages/allure-vitest/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME } = env;
export default class AllureReporter implements Reporter {
private allureRuntime: AllureRuntime;
private options: AllureReporterOptions;
private hostname: string = ALLURE_HOST_NAME || hostname();

constructor(options: AllureReporterOptions) {
this.options = options;
Expand Down Expand Up @@ -113,7 +114,7 @@ export default class AllureReporter implements Reporter {
const testDisplayName = currentTest.displayName || titleMetadata.cleanTitle;
const links = currentTest.links ? this.processMetadataLinks(currentTest.links) : [];
const labels: Label[] = [].concat(currentTest.labels || []).concat(titleMetadata.labels);
const test = parent.startTest(testDisplayName);
const test = parent.startTest(testDisplayName, task.result.startTime);
const suitePath = getSuitePath(task);
const normalizedTestPath = normalize(relative(cwd(), task.file.filepath))
.replace(/^\//, "")
Expand All @@ -129,7 +130,7 @@ export default class AllureReporter implements Reporter {
test.addLabel(LabelName.FRAMEWORK, "vitest");
test.addLabel(LabelName.LANGUAGE, "javascript");
test.addLabel(LabelName.THREAD, ALLURE_THREAD_NAME || pid.toString());
test.addLabel(LabelName.HOST, ALLURE_HOST_NAME || hostname.toString());
test.addLabel(LabelName.HOST, ALLURE_HOST_NAME || this.hostname.toString());

getSuitesLabels(suitePath).forEach((label) => {
test.addLabel(label.name, label.value);
Expand Down Expand Up @@ -158,8 +159,8 @@ export default class AllureReporter implements Reporter {
break;
}
}

const endTime = task.result ? task.result.startTime + task.result.duration : undefined;
test.calculateHistoryId();
test.endTest(task.result?.duration);
test.endTest(endTime);
}
}
11 changes: 10 additions & 1 deletion packages/allure-vitest/test/spec/direct/timeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ it("adds check thread and hostname", async () => {
import { test } from "vitest";
import { attachment } from "allure-vitest";

test("text attachment", async (t) => {});
function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}

test("text attachment", async (t) => {
await sleep(10);
});
`);

expect(tests).toHaveLength(1);
expect(tests[0].labels).toContainEqual({ name: LabelName.HOST, value: expect.any(String) });
expect(tests[0].labels).toContainEqual({ name: LabelName.THREAD, value: expect.any(String) });
expect(tests[0].start || 0).toBeLessThan(tests[0].stop || 0);
});
Loading