Skip to content

Commit

Permalink
fix step contexts in playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Feb 11, 2025
1 parent 9f50c6a commit 851c4c2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/allure-js-commons/src/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type StepContext = {
parameter: (name: string, value: string, mode?: ParameterMode) => void | PromiseLike<void>;
};

const stepContext: () => StepContext = () => ({
export const stepContext: () => StepContext = () => ({
displayName: (name: string) => {
return callRuntimeMethod("stepDisplayName", name);
},
Expand Down
36 changes: 12 additions & 24 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ export class AllureReporter implements ReporterV2 {
const testUuid = this.allureResultsUuids.get(test.id)!;
const isRootBeforeHook = step.title === BEFORE_HOOKS_ROOT_STEP_TITLE;
const isRootAfterHook = step.title === AFTER_HOOKS_ROOT_STEP_TITLE;
const isRootHook = isRootBeforeHook || isRootAfterHook;
const isBeforeHookDescendant = isBeforeHookStep(step);
const isAfterHookDescendant = isAfterHookStep(step);
const isAfterHook = isRootAfterHook || isAfterHookDescendant;
Expand All @@ -386,29 +385,6 @@ export class AllureReporter implements ReporterV2 {
stack.stopStep({
duration: step.duration,
});
}

if (isRootHook) {
const stack = isRootAfterHook
? this.afterHooksStepsStack.get(test.id)!
: this.beforeHooksStepsStack.get(test.id)!;

this.allureRuntime?.updateTest(testUuid, (testResult) => {
if (isRootAfterHook) {
testResult.steps.push(...stack.steps);
} else {
testResult.steps.unshift(...stack.steps);
}
});

if (isRootAfterHook) {
this.afterHooksStepsStack.delete(test.id);
} else {
this.beforeHooksStepsStack.delete(test.id);
}
}

if (isHook) {
return;
}

Expand Down Expand Up @@ -439,6 +415,8 @@ export class AllureReporter implements ReporterV2 {
const error = result.error;
// only apply default suites if not set by user
const [, projectSuiteTitle, fileSuiteTitle, ...suiteTitles] = test.parent.titlePath();
const beforeHooksStack = this.beforeHooksStepsStack.get(test.id);
const afterHooksStack = this.afterHooksStepsStack.get(test.id);

this.allureRuntime!.updateTest(testUuid, (testResult) => {
testResult.labels.push(getHostLabel());
Expand Down Expand Up @@ -531,6 +509,16 @@ export class AllureReporter implements ReporterV2 {
return labelsGroup;
});

if (beforeHooksStack) {
testResult.steps.unshift(...beforeHooksStack.steps);
this.beforeHooksStepsStack.delete(test.id);
}

if (afterHooksStack) {
testResult.steps.push(...afterHooksStack.steps);
this.afterHooksStepsStack.delete(test.id);
}

testResult.labels = newLabels;
});

Expand Down
4 changes: 0 additions & 4 deletions packages/allure-playwright/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime {
super();
}

async step<T = void>(name: string, body: () => T | PromiseLike<T>) {
return await test.step(name, () => Promise.resolve(body()));
}

async attachment(name: string, content: Buffer | string, options: AttachmentOptions) {
await test.info().attach(name, { body: content, contentType: options.contentType });
}
Expand Down
46 changes: 46 additions & 0 deletions packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,49 @@ it("handles nested lambda steps", async () => {
stage: Stage.FINISHED,
});
});

it("should allow to set step metadata through its context", async () => {
const { tests } = await runPlaywrightInlineTest({
"sample.test.ts": `
import { test, allure } from "allure-playwright";
test("steps", async () => {
await allure.step("step 1", async () => {
await allure.step("step 2", async () => {
await allure.step("step 3", async (stepContext) => {
await stepContext.displayName("custom name");
await stepContext.parameter("param", "value");
});
});
});
});
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].steps).toHaveLength(3);
expect(tests[0].steps[0]).toMatchObject({
name: "Before Hooks",
});
expect(tests[0].steps[1]).toMatchObject({
name: "step 1",
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[1].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0]).toMatchObject({
name: "step 2",
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
name: "custom name",
parameters: [expect.objectContaining({ name: "param", value: "value" })],
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[2]).toMatchObject({
name: "After Hooks",
});
});
47 changes: 47 additions & 0 deletions packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,50 @@ it("should support log steps", async () => {
]),
);
});

it("should allow to set step metadata through its context", async () => {
const { tests } = await runPlaywrightInlineTest({
"sample.test.ts": `
import { test } from "allure-playwright";
import { step } from "allure-js-commons";
test("steps", async () => {
await step("step 1", async () => {
await step("step 2", async () => {
await step("step 3", async (stepContext) => {
await stepContext.displayName("custom name");
await stepContext.parameter("param", "value");
});
});
});
});
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].steps).toHaveLength(3);
expect(tests[0].steps[0]).toMatchObject({
name: "Before Hooks",
});
expect(tests[0].steps[1]).toMatchObject({
name: "step 1",
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[1].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0]).toMatchObject({
name: "step 2",
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
name: "custom name",
parameters: [expect.objectContaining({ name: "param", value: "value" })],
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(tests[0].steps[2]).toMatchObject({
name: "After Hooks",
});
});

0 comments on commit 851c4c2

Please sign in to comment.