Skip to content

Commit ee0a29e

Browse files
authored
Fix playwright runtime metadata messages processing (#1327)
1 parent 6e90057 commit ee0a29e

File tree

2 files changed

+102
-22
lines changed

2 files changed

+102
-22
lines changed

packages/allure-playwright/src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint max-lines: off */
12
import type { FullConfig } from "@playwright/test";
23
import type { TestResult as PlaywrightTestResult, Suite, TestCase, TestStep } from "@playwright/test/reporter";
34
import { existsSync } from "node:fs";
@@ -304,7 +305,9 @@ export class AllureReporter implements ReporterV2 {
304305

305306
if (["test.attach", "attach"].includes(step.category)) {
306307
stack.startStep(baseStep);
308+
307309
const attachStack = isBeforeHookDescendant ? this.beforeHooksAttachmentsStack : this.afterHooksAttachmentsStack;
310+
308311
stack.updateStep((stepResult) => {
309312
stepResult.name = normalizeHookTitle(stepResult.name!);
310313
stepResult.stage = Stage.FINISHED;
@@ -432,12 +435,19 @@ export class AllureReporter implements ReporterV2 {
432435
.map((hookStep) => normalizeHookTitle(hookStep.title))
433436
.filter(Boolean),
434437
);
438+
const hookAttachmentNames = new Set(
439+
[...attachmentsInBeforeHooks, ...attachmentsInAfterHooks]
440+
.map((hookStep) => normalizeHookTitle(hookStep.title))
441+
.filter(Boolean),
442+
);
435443
const attachmentSteps = this.attachmentSteps.get(testUuid) ?? [];
436444
const attachmentsInSteps = result.attachments.filter((attachment) => !hookAttachmentUuids.has(attachment.name));
445+
const onlyHooksAttachments = result.attachments.filter((att) => hookAttachmentNames.has(att.name));
437446

438447
for (let i = 0; i < attachmentsInSteps.length; i++) {
439448
const attachment = attachmentsInSteps[i];
440449
const attachmentStep = attachmentSteps.length > i ? attachmentSteps[i] : undefined;
450+
441451
await this.processAttachment(testUuid, attachmentStep, attachment);
442452
}
443453

@@ -468,13 +478,6 @@ export class AllureReporter implements ReporterV2 {
468478
// FIXME: temp logic for labels override, we need it here to keep the reporter compatible with v2 API
469479
// in next iterations we need to implement the logic for every javascript integration
470480

471-
const hookAttachmentNames = new Set(
472-
[...attachmentsInBeforeHooks, ...attachmentsInAfterHooks]
473-
.map((hookStep) => normalizeHookTitle(hookStep.title))
474-
.filter(Boolean),
475-
);
476-
const onlyHooksAttachments = result.attachments.filter((att) => hookAttachmentNames.has(att.name));
477-
478481
for (const attachment of onlyHooksAttachments) {
479482
const matchingBeforeHookStep = attachmentsInBeforeHooks.find(
480483
(step) => normalizeHookTitle(step.title) === attachment.name,
@@ -485,6 +488,11 @@ export class AllureReporter implements ReporterV2 {
485488
const targetStack = matchingBeforeHookStep ? beforeHooksStack : afterHooksStack;
486489
const hookStep = matchingBeforeHookStep || matchingAfterHookStep;
487490

491+
if (attachment.contentType === ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE) {
492+
await this.processAttachment(testUuid, hookStep?.uuid, attachment);
493+
continue;
494+
}
495+
488496
if (targetStack && hookStep) {
489497
const stepResult = targetStack?.findStepByUuid(hookStep?.uuid);
490498
if (stepResult) {
@@ -497,6 +505,7 @@ export class AllureReporter implements ReporterV2 {
497505
}
498506
}
499507
}
508+
500509
this.allureRuntime!.updateTest(testUuid, (testResult) => {
501510
const mappedLabels = testResult.labels.reduce<Record<string, Label[]>>((acc, label) => {
502511
if (!acc[label.name]) {
@@ -533,7 +542,6 @@ export class AllureReporter implements ReporterV2 {
533542

534543
testResult.labels = newLabels;
535544
});
536-
537545
this.allureRuntime!.stopTest(testUuid, { duration: result.duration });
538546
this.allureRuntime!.writeTest(testUuid);
539547
}

packages/allure-playwright/test/spec/hooks.spec.ts

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,27 @@ it("keeps correct hooks structure when something failed", async () => {
179179
it("should hook steps have attachments", async () => {
180180
const results = await runPlaywrightInlineTest({
181181
"sample.test.js": `
182-
const {test} = require("@playwright/test");
182+
const { test } = require("@playwright/test");
183183
const allure = require("allure-js-commons");
184+
184185
test.beforeAll(async () => {
185-
await allure.attachment('attachment outside step beforeAll', 'test value', 'application/json');
186-
await allure.step('i am beforeAll step', async () => {
187-
await allure.attachment('attachment in beforeAll step', 'test value', 'application/json');
188-
});
186+
await allure.attachment('attachment outside step beforeAll', 'test value', 'application/json');
187+
await allure.step('i am beforeAll step', async () => {
188+
await allure.attachment('attachment in beforeAll step', 'test value', 'application/json');
189+
});
189190
});
191+
190192
test.afterAll(async () => {
191-
await allure.attachment('attachment outside step afterAll', 'test value', 'application/json');
192-
await allure.step('i am afterAll', async () => {
193-
await allure.attachment('test key afterall', 'test value', 'application/json');
194-
});
193+
await allure.attachment('attachment outside step afterAll', 'test value', 'application/json');
194+
await allure.step('i am afterAll', async () => {
195+
await allure.attachment('test key afterall', 'test value', 'application/json');
196+
});
195197
});
198+
196199
test("sample test", async () => {
197-
await allure.step("step 1", async () => {
198-
await allure.attachment('attach in step 1', 'test value', 'application/json');
199-
});
200+
await allure.step("step 1", async () => {
201+
await allure.attachment('attach in step 1', 'test value', 'application/json');
202+
});
200203
});
201204
`,
202205
"playwright.config.js": `
@@ -222,11 +225,13 @@ it("should hook steps have attachments", async () => {
222225
`,
223226
});
224227
const steps = results.tests[0].steps;
228+
225229
expect(steps).toHaveLength(3);
230+
226231
const beforeHooks = steps[0];
227232
const beforeAllHook = beforeHooks.steps[0];
228-
expect(beforeAllHook.name).equal("beforeAll hook");
229233

234+
expect(beforeAllHook.name).equal("beforeAll hook");
230235
expect(beforeAllHook.steps).toEqual([
231236
expect.objectContaining({
232237
name: "attachment outside step beforeAll",
@@ -254,8 +259,8 @@ it("should hook steps have attachments", async () => {
254259
]);
255260

256261
const step1Attachment = steps[1];
257-
expect(step1Attachment.name).toEqual("step 1");
258262

263+
expect(step1Attachment.name).toEqual("step 1");
259264
expect(step1Attachment.steps).toEqual([
260265
expect.objectContaining({
261266
name: "attach in step 1",
@@ -269,6 +274,7 @@ it("should hook steps have attachments", async () => {
269274
]);
270275

271276
const afterHooks = steps[2];
277+
272278
expect(afterHooks.steps[0].steps).toEqual([
273279
expect.objectContaining({
274280
name: "attachment outside step afterAll",
@@ -295,3 +301,69 @@ it("should hook steps have attachments", async () => {
295301
}),
296302
]);
297303
});
304+
305+
it("should not loose tests metadata when when there are hooks in the test", async () => {
306+
const results = await runPlaywrightInlineTest({
307+
"sample.test.js": `
308+
import test from '@playwright/test';
309+
import * as allure from "allure-js-commons";
310+
311+
test.beforeAll(async () => {
312+
await allure.label("hook", "1");
313+
});
314+
315+
test.beforeEach(async () => {
316+
await allure.label("hook", "2");
317+
});
318+
319+
test.afterAll(async () => {
320+
await allure.label("hook", "3");
321+
});
322+
323+
test.afterEach(async () => {
324+
await allure.label("hook", "4");
325+
});
326+
327+
test("should contain label", async ({ page }) => {
328+
await allure.label("hook", "5");
329+
});
330+
`,
331+
});
332+
333+
expect(results.tests).toHaveLength(1);
334+
expect(results.tests[0].labels).toEqual(
335+
expect.arrayContaining([
336+
{
337+
name: "hook",
338+
value: "1",
339+
},
340+
{
341+
name: "hook",
342+
value: "2",
343+
},
344+
{
345+
name: "hook",
346+
value: "3",
347+
},
348+
{
349+
name: "hook",
350+
value: "4",
351+
},
352+
{
353+
name: "hook",
354+
value: "5",
355+
},
356+
]),
357+
);
358+
expect(results.tests[0].steps).toHaveLength(2);
359+
expect(results.tests[0].steps).toEqual(
360+
expect.arrayContaining([
361+
expect.objectContaining({
362+
name: "Before Hooks",
363+
}),
364+
expect.objectContaining({
365+
name: "After Hooks",
366+
}),
367+
]),
368+
);
369+
});

0 commit comments

Comments
 (0)