Skip to content

Commit

Permalink
fix(vitest): fix thread id for tests (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
vovsemenv authored Feb 16, 2024
1 parent 64d0dd6 commit 95cac06
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/allure-vitest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface AllureVitestApi {
interface AllureTestMeta extends vitest.TaskMeta {
currentTest: MetadataMessage;
currentStep?: StepMetadata;
VITEST_POOL_ID?: string;
}

declare global {
Expand All @@ -47,6 +48,7 @@ const injectAllureMeta = (context: vitest.TaskContext) => {
}

(context.task.meta as AllureTestMeta) = {
...context.task.meta,
currentTest: {
displayName: context.task.name || "root-test",
labels: [],
Expand Down
15 changes: 11 additions & 4 deletions packages/allure-vitest/src/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hostname } from "node:os";
import { basename, normalize, relative } from "node:path";
import { cwd, env, pid } from "node:process";
import { cwd, env } from "node:process";
import { File, Reporter, Suite, Task, Vitest } from "vitest";
import {
AllureGroup,
Expand Down Expand Up @@ -102,7 +102,10 @@ export default class AllureReporter implements Reporter {
return;
}

const { currentTest = {} } = task.meta as { currentTest: MetadataMessage };
const { currentTest = {}, VITEST_POOL_ID } = task.meta as {
currentTest: MetadataMessage;
VITEST_POOL_ID: string;
};
const skippedByTestPlan = currentTest.labels?.some(({ name }) => name === ALLURE_SKIPPED_BY_TEST_PLAN_LABEL);

// do not report tests skipped by test plan
Expand All @@ -129,8 +132,12 @@ 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 || this.hostname.toString());
test.addLabel(LabelName.HOST, this.hostname);

const thread_id = ALLURE_THREAD_NAME || (VITEST_POOL_ID && `${this.hostname}-vitest-worker-${VITEST_POOL_ID}`);
if (thread_id) {
test.addLabel(LabelName.THREAD, thread_id);
}

getSuitesLabels(suitePath).forEach((label) => {
test.addLabel(label.name, label.value);
Expand Down
5 changes: 5 additions & 0 deletions packages/allure-vitest/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ beforeEach(async (ctx) => {
return;
}

(ctx.task as any).meta = {
...ctx.task.meta,
VITEST_POOL_ID: process.env.VITEST_POOL_ID,
};

// @ts-ignore
global.allure = allureAPI;
});
Expand Down
17 changes: 13 additions & 4 deletions packages/allure-vitest/test/spec/direct/timeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { LabelName } from "allure-js-commons";
import { runVitestInlineTest } from "../../utils.js";

it("adds check thread and hostname", async () => {
const { tests } = await runVitestInlineTest(`
const testHostname = "test-runner-host";
const { tests } = await runVitestInlineTest(
`
import { test } from "vitest";
import { attachment } from "allure-vitest";
Expand All @@ -16,10 +18,17 @@ it("adds check thread and hostname", async () => {
test("text attachment", async (t) => {
await sleep(10);
});
`);
`,
undefined,
// eslint-disable-next-line @typescript-eslint/require-await
async () => {
process.env.ALLURE_HOST_NAME = testHostname;
},
);

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);

expect(tests[0].labels).toContainEqual({ name: LabelName.HOST, value: expect.any(String) });
expect(tests[0].labels).toContainEqual({ name: LabelName.THREAD, value: `${testHostname}-vitest-worker-1` });
});

0 comments on commit 95cac06

Please sign in to comment.