Skip to content

Commit

Permalink
new fullname format support for playwright testplans with old one fal…
Browse files Browse the repository at this point in the history
…lback
  • Loading branch information
epszaw committed May 23, 2024
1 parent 7c5a4a4 commit 5a68677
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 60 deletions.
10 changes: 6 additions & 4 deletions packages/allure-js-commons/src/sdk/TestPlan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface TestPlanV1Test {
id: string | number;
selector: string;
}

export interface TestPlanV1 {
version: "1.0";
tests: {
id: string | number;
selector: string;
}[];
tests: TestPlanV1Test[];
}
4 changes: 2 additions & 2 deletions packages/allure-js-commons/src/sdk/node/TestPlan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from "node:fs";
import { TestPlanV1 } from "../TestPlan.js";
import { TestPlanV1, TestPlanV1Test } from "../TestPlan.js";

export { TestPlanV1 };
export { TestPlanV1, TestPlanV1Test };

export const parseTestPlan = (): TestPlanV1 | undefined => {
const testPlanPath = process.env.ALLURE_TESTPLAN_PATH;
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-js-commons/src/sdk/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export * from "../context/index.js";
export { AllureNodeReporterRuntime } from "./ReporterRuntime.js";
export { MessageAllureWriter, FileSystemAllureWriter, AllureInMemoryWriter } from "./writers/index.js";
export { AllureNodeCrypto } from "./Crypto.js";
export { parseTestPlan, TestPlanV1 } from "./TestPlan.js";
export { parseTestPlan, TestPlanV1, TestPlanV1Test } from "./TestPlan.js";
export { ALLURE_TEST_RUNTIME_KEY, getGlobalTestRuntime, setGlobalTestRuntime } from "../../TestRuntime.js";
export { readImageAsBase64 } from "../utils.js";
67 changes: 62 additions & 5 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
RuntimeMessage,
Stage,
Status,
TestPlanV1Test,
TestResult,
escapeRegExp,
extractMetadataFromString,
parseTestPlan,
readImageAsBase64,
Expand All @@ -38,17 +40,29 @@ const stepAttachPrefixLength = 50;

interface ReporterV2 {
onConfigure(config: FullConfig): void;

onBegin(suite: Suite): void;

onTestBegin(test: TestCase, result: PlaywrightTestResult): void;

onStdOut(chunk: string | Buffer, test?: TestCase, result?: PlaywrightTestResult): void;

onStdErr(chunk: string | Buffer, test?: TestCase, result?: PlaywrightTestResult): void;

onTestEnd(test: TestCase, result: PlaywrightTestResult): void;

onEnd(result: FullResult): Promise<{ status?: FullResult["status"] } | undefined | void> | void;

onExit(): void | Promise<void>;

onError(error: TestError): void;

onStepBegin(test: TestCase, result: PlaywrightTestResult, step: TestStep): void;

onStepEnd(test: TestCase, result: PlaywrightTestResult, step: TestStep): void;

printsToStdio(): boolean;

version(): "v2";
}

Expand All @@ -70,14 +84,57 @@ export class AllureReporter implements ReporterV2 {

onConfigure(config: FullConfig): void {
this.config = config;

const testPlan = parseTestPlan();
if (testPlan) {
// @ts-ignore
const configElement = config[Object.getOwnPropertySymbols(config)[0]];
if (configElement) {
configElement.cliArgs = testPlan.tests.filter((test) => test.selector).map((test) => test.selector);

if (!testPlan) {
return;
}

// @ts-ignore
const configElement = config[Object.getOwnPropertySymbols(config)[0]];

if (!configElement) {
return;
}

const testsWithSelectors = testPlan.tests.filter((test) => test.selector);
const v1ReporterTests: TestPlanV1Test[] = [];
const v2ReporterTests: TestPlanV1Test[] = [];
const cliArgs: string[] = [];

testsWithSelectors.forEach((test) => {
if (!/#/.test(test.selector as string)) {
v2ReporterTests.push(test);
return;
}

v1ReporterTests.push(test);
});

if (v2ReporterTests.length) {
// we need to cut off column because playwright works only with line number
const v2SelectorsArgs = v2ReporterTests
.map((test) => test.selector.replace(/:\d+$/, ""))
.map((selector) => escapeRegExp(selector));

cliArgs.push(...(v2SelectorsArgs as string[]));
}

if (v1ReporterTests.length) {
const v1SelectorsArgs = v1ReporterTests
// we can filter tests only by absolute path, so we need to cut off test name
.map((test) => test.selector.split("#")[0])
.map((selector) => escapeRegExp(selector));

cliArgs.push(...(v1SelectorsArgs as string[]));
}

if (!cliArgs.length) {
return;
}

configElement.cliArgs = cliArgs;
}

onError(): void {}
Expand Down
182 changes: 134 additions & 48 deletions packages/allure-playwright/test/spec/testplan.spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,150 @@
import { expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import { TestPlanV1 } from "allure-js-commons/sdk/node";
import { runPlaywrightInlineTest } from "../utils";

it("respects testplan", async () => {
const exampleTestPlan: TestPlanV1 = {
version: "1.0",
tests: [
describe("testplan with v1 reporter full names", () => {
it("respects testplan", async () => {
const exampleTestPlan: TestPlanV1 = {
version: "1.0",
tests: [
{
id: 1,
selector: "nested/super strange nested/super strange name.test.ts#also nested should execute",
},
{
id: 2,
selector: "b.test.ts#should execute",
},
{
id: 3,
// Wierd Regexp selector that should be escaped and match only one test
selector: ".+.test.ts#+.",
},
{
id: 4,
selector: "notaga.test.ts#a",
},
],
};
const testPlanFilename = "example-testplan.json";
const results = await runPlaywrightInlineTest(
{
id: 1,
selector: "nested/super strange nested/super strange name.test.ts#also nested should execute",
},
{
id: 2,
selector: "b.test.ts#should execute",
},
{
id: 3,
// Wierd Regexp selector that should be escaped and match only one test
selector: ".+.test.ts#+.",
[testPlanFilename]: JSON.stringify(exampleTestPlan),
"a.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('should not execute', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"b.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('should execute', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"nested/super strange nested/super strange name.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test.describe('also nested', () => {
test('should execute', async ({}, testInfo) => {
});
});
`,
".+.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('+.', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"aga.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('a', async ({}, testInfo) => {
expect(1).toBe(1);
});
test('aa', async ({}, testInfo) => {
expect(1).toBe(1);
});
test('selected name @allure.id=5', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"notaga.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('a', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
},
[],
{
id: 4,
selector: "aga.test.ts#a",
ALLURE_TESTPLAN_PATH: testPlanFilename,
},
);

expect(results.tests.map((value) => value.fullName)).toEqual(
expect.arrayContaining([
"b.test.ts:3:13",
"nested/super strange nested/super strange name.test.ts:4:14",
".+.test.ts:3:13",
"notaga.test.ts:3:13",
]),
);
});
});

describe("testplan with v2 reporter full names", () => {
it("respects testplan", async () => {
const exampleTestPlan: TestPlanV1 = {
version: "1.0",
tests: [
{
id: 1,
selector: "nested/super strange nested/super strange name.test.ts:4:14",
},
{
id: 2,
selector: "b.test.ts:3:13",
},
{
id: 3,
// Wierd Regexp selector that should be escaped and match only one test
selector: ".+.test.ts:3:13",
},
{
id: 4,
selector: "notaga.test.ts:3:13",
},
],
};
const testPlanFilename = "example-testplan.json";
const results = await runPlaywrightInlineTest(
{
id: 5,
selector: "aga.test.ts#selected name @allure.id=5",
},
],
};
const testPlanFilename = "example-testplan.json";
const results = await runPlaywrightInlineTest(
{
[testPlanFilename]: JSON.stringify(exampleTestPlan),
"a.test.ts": /* ts */ `
[testPlanFilename]: JSON.stringify(exampleTestPlan),
"a.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('should not execute', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"b.test.ts": /* ts */ `
"b.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('should execute', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"nested/super strange nested/super strange name.test.ts": /* ts */ `
"nested/super strange nested/super strange name.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test.describe('also nested', () => {
test('should execute', async ({}, testInfo) => {
});
});
`,
".+.test.ts": /* ts */ `
".+.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('+.', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
"aga.test.ts": /* ts */ `
"aga.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('a', async ({}, testInfo) => {
expect(1).toBe(1);
Expand All @@ -70,26 +156,26 @@ it("respects testplan", async () => {
expect(1).toBe(1);
});
`,
"notaga.test.ts": /* ts */ `
"notaga.test.ts": /* ts */ `
import { test, expect } from '@playwright/test';
test('a', async ({}, testInfo) => {
expect(1).toBe(1);
});
`,
},
[],
{
ALLURE_TESTPLAN_PATH: testPlanFilename,
},
);
},
[],
{
ALLURE_TESTPLAN_PATH: testPlanFilename,
},
);

expect(results.tests.map((value) => value.fullName)).toEqual(
expect.arrayContaining([
"b.test.ts#should execute",
".+.test.ts#+.",
"nested/super strange nested/super strange name.test.ts#also nested should execute",
"aga.test.ts#a",
"aga.test.ts#selected name @allure.id=5",
]),
);
expect(results.tests.map((value) => value.fullName)).toEqual(
expect.arrayContaining([
"b.test.ts:3:13",
"nested/super strange nested/super strange name.test.ts:4:14",
".+.test.ts:3:13",
"notaga.test.ts:3:13",
]),
);
});
});

0 comments on commit 5a68677

Please sign in to comment.