Skip to content

Commit

Permalink
add method to update shallow stack's steps
Browse files Browse the repository at this point in the history
change stop method signature
complete new hooks reporting logic for pw
fix failed pw tests
  • Loading branch information
epszaw committed Feb 7, 2025
1 parent cec96d3 commit b5504ab
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 77 deletions.
21 changes: 13 additions & 8 deletions packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,27 @@ export class ShallowStepsStack {
this.#runningSteps.push(stepResult);
}

updateStep(step?: Partial<StepResult>) {
updateStep(updateFunc: (result: StepResult) => void) {
if (!this.#currentStep) {
throw new Error("there is no step to stop");
// eslint-disable-next-line no-console
console.error("There is no running step in the stack to update!");
return;
}

Object.assign(this.#currentStep, step);
updateFunc(this.#currentStep);
}

stopStep(step?: Partial<StepResult>) {
stopStep(opts?: { stop?: number; duration?: number }) {
if (!this.#currentStep) {
throw new Error("there is no step to stop");
// eslint-disable-next-line no-console
console.error("There is no running step in the stack to stop!");
return;
}

Object.assign(this.#currentStep, {
stage: Stage.FINISHED,
...step,
const { stop, duration = 0 } = opts ?? {};

this.updateStep((result) => {
result.stop = stop ?? result.start ? result.start! + duration : undefined;
});

this.#runningSteps.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,15 @@ describe("ReporterRuntime", () => {
const stack = new ShallowStepsStack();

stack.startStep({ name: "step1" });
stack.stopStep({ status: Status.PASSED });
stack.updateStep((result) => {
result.status = Status.PASSED;
result.stage = Stage.FINISHED;
});
stack.stopStep();

expect(stack.steps).toEqual([
expect.objectContaining({
name: "step1",
status: Status.PASSED,
stage: Stage.FINISHED,
steps: [],
}),
]);
Expand Down
86 changes: 45 additions & 41 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,36 +320,31 @@ export class AllureReporter implements ReporterV2 {
start: step.startTime.getTime(),
stage: Stage.RUNNING,
};
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);

if (isBeforeHookDescendant) {
const stack = this.beforeHooksStepsStack.get(test.id)!;
if (isBeforeHookDescendant || isAfterHookDescendant) {
const stack = isBeforeHookDescendant
? this.beforeHooksStepsStack.get(test.id)!
: this.afterHooksStepsStack.get(test.id)!;

stack.startStep(baseStep);
return;
}

if (isAfterHookDescendant) {
const stack = this.afterHooksStepsStack.get(test.id)!;

stack.startStep(baseStep);
return;
}

if (step.title === BEFORE_HOOKS_ROOT_STEP_TITLE) {
if (isRootHook) {
const stack = new ShallowStepsStack();

stack.startStep(baseStep);
this.beforeHooksStepsStack.set(test.id, stack);
return;
}

if (step.title === AFTER_HOOKS_ROOT_STEP_TITLE) {
const stack = new ShallowStepsStack();

stack.startStep(baseStep);
this.afterHooksStepsStack.set(test.id, stack);
if (isRootBeforeHook) {
this.beforeHooksStepsStack.set(test.id, stack);
} else {
this.afterHooksStepsStack.set(test.id, stack);
}
return;
}

Expand All @@ -367,44 +362,53 @@ 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;
const isHook = isRootBeforeHook || isRootAfterHook || isBeforeHookDescendant || isAfterHookDescendant;

if (isBeforeHookDescendant) {
const stack = this.beforeHooksStepsStack.get(test.id)!;
if (isHook) {
const stack = isAfterHook ? this.afterHooksStepsStack.get(test.id)! : this.beforeHooksStepsStack.get(test.id)!;

stack.stopStep({
stage: Stage.FINISHED,
});
return;
}
stack.updateStep((stepResult) => {
const { status = Status.PASSED } = getWorstTestStepResult(stepResult.steps) ?? {};

if (isAfterHookDescendant) {
const stack = this.afterHooksStepsStack.get(test.id)!;
stepResult.status = step.error ? Status.FAILED : status;
stepResult.stage = Stage.FINISHED;

if (step.error) {
stepResult.statusDetails = { ...getMessageAndTraceFromError(step.error) };
}
});
stack.stopStep({
stage: Stage.FINISHED,
duration: step.duration,
});
return;
}

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

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

if (step.title === AFTER_HOOKS_ROOT_STEP_TITLE) {
const stack = this.afterHooksStepsStack.get(test.id)!;
if (isRootAfterHook) {
this.afterHooksStepsStack.delete(test.id);
} else {
this.beforeHooksStepsStack.delete(test.id);
}
}

this.allureRuntime?.updateTest(testUuid, (testResult) => {
testResult.steps.push(...stack.steps);
});
this.afterHooksStepsStack.delete(test.id);
if (isHook) {
return;
}

Expand Down
44 changes: 19 additions & 25 deletions packages/allure-playwright/test/spec/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ it("handles before hooks", async () => {
const [beforeHooks] = tests[0].steps;

expect(beforeHooks).toMatchObject({
name: "Before Hooks",
steps: expect.arrayContaining([
expect.objectContaining({
name: "beforeAll hook",
Expand Down Expand Up @@ -95,31 +96,24 @@ it("should mark step as failed when any child step is failed", async () => {
`,
});

expect(results.tests[0]).toEqual(
expect.objectContaining({
name: "should contain hooks",
status: Status.BROKEN,
steps: [
expect.objectContaining({
name: "Before Hooks",
status: Status.PASSED,
}),

expect.objectContaining({
name: "page.waitForEvent",
status: Status.FAILED,
}),
expect.objectContaining({
name: "Worker Cleanup",
status: Status.PASSED,
}),
expect.objectContaining({
name: "After Hooks",
status: Status.FAILED,
}),
],
}),
);
expect(results.tests[0]).toMatchObject({
name: "should contain hooks",
status: Status.BROKEN,
steps: [
expect.objectContaining({
name: "Before Hooks",
status: Status.PASSED,
}),
expect.objectContaining({
name: "page.waitForEvent",
status: Status.FAILED,
}),
expect.objectContaining({
name: "After Hooks",
status: Status.PASSED,
}),
],
});
});

it("keeps correct hooks structure when something failed", async () => {
Expand Down

0 comments on commit b5504ab

Please sign in to comment.