Skip to content

Commit

Permalink
feat(allure-playwright): add package label (#1033)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Baev <[email protected]>
  • Loading branch information
baev and baev authored Jul 2, 2024
1 parent 539d3f1 commit 2b7fa73
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import { existsSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import process from "node:process";
import type { ImageDiffAttachment, Label, TestResult } from "allure-js-commons";
import { ContentType, LabelName, Stage, Status } from "allure-js-commons";
import {
ContentType,
type ImageDiffAttachment,
type Label,
LabelName,
Stage,
Status,
type TestResult,
} from "allure-js-commons";
import type { RuntimeMessage, TestPlanV1Test } from "allure-js-commons/sdk";
import { extractMetadataFromString, getMessageAndTraceFromError, hasLabel, stripAnsi } from "allure-js-commons/sdk";
import {
Expand Down Expand Up @@ -163,14 +170,15 @@ export class AllureReporter implements ReporterV2 {
const suite = test.parent;
const titleMetadata = extractMetadataFromString(test.title);
const project = suite.project()!;
const relativeFile = path.relative(project?.testDir, test.location.file).split(path.sep).join("/");
const pathElements = path.relative(project?.testDir, test.location.file).split(path.sep);
const relativeFile = pathElements.join("/");
// root > project > file path > test.describe...
const [, , , ...suiteTitles] = suite.titlePath();
const nameSuites = suiteTitles.length > 0 ? `${suiteTitles.join(" ")} ` : "";
const testCaseIdBase = `${relativeFile}#${nameSuites}${test.title}`;
const result: Partial<TestResult> = {
name: titleMetadata.cleanTitle,
labels: titleMetadata.labels,
labels: [...titleMetadata.labels],
links: [],
parameters: [],
testCaseId: md5(testCaseIdBase),
Expand All @@ -180,6 +188,7 @@ export class AllureReporter implements ReporterV2 {
result.labels!.push({ name: LabelName.LANGUAGE, value: "JavaScript" });
result.labels!.push({ name: LabelName.FRAMEWORK, value: "Playwright" });
result.labels!.push({ name: "titlePath", value: suite.titlePath().join(" > ") });
result.labels!.push({ name: LabelName.PACKAGE, value: pathElements.join(".") });

if (project?.name) {
result.parameters!.push({ name: "Project", value: project.name });
Expand Down
39 changes: 39 additions & 0 deletions packages/allure-playwright/test/spec/package.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, it } from "vitest";
import { runPlaywrightInlineTest } from "../utils.js";

it("should set package label", async () => {
const { tests } = await runPlaywrightInlineTest({
"some/path/to/sample.test.js": `
import { test } from '@playwright/test';
test('test 1', async () => {});
`,
"some/other/to/some.test.js": `
import { test } from '@playwright/test';
test('test 2', async () => {});
`,
"root.test.js": `
import { test } from '@playwright/test';
test('test 3', async () => {});
`,
});

expect(tests).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "test 1",
labels: expect.arrayContaining([{ name: "package", value: "some.path.to.sample.test.js" }]),
}),
expect.objectContaining({
name: "test 2",
labels: expect.arrayContaining([{ name: "package", value: "some.other.to.some.test.js" }]),
}),
expect.objectContaining({
name: "test 3",
labels: expect.arrayContaining([{ name: "package", value: "root.test.js" }]),
}),
]),
);
});

0 comments on commit 2b7fa73

Please sign in to comment.