Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(allure-cucumberjs): add support for playwright assertions #1161

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions packages/allure-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { extname } from "node:path";
import type { Label, Link, TestResult } from "allure-js-commons";
import { ContentType, LabelName, Stage, Status } from "allure-js-commons";
import { getMessageAndTraceFromError } from "allure-js-commons/sdk";
import { getMessageAndTraceFromError, getStatusFromError } from "allure-js-commons/sdk";
import {
ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
ReporterRuntime,
Expand All @@ -25,7 +25,7 @@ import {
getLanguageLabel,
getPackageLabel,
getThreadLabel,
getWorstStepResultStatus,
getWorstTestStepResult,
md5,
} from "allure-js-commons/sdk/reporter";
import { AllureCucumberWorld } from "./legacy.js";
Expand Down Expand Up @@ -180,11 +180,10 @@ export default class AllureCucumberReporter extends Formatter {
}

private parseStatus(stepResult: TestStepResult): Status | undefined {
const containsAssertionError = /assertion/i.test(stepResult?.exception?.type || "");

const error = this.exceptionToError(stepResult.message, stepResult.exception);
switch (stepResult.status) {
case TestStepResultStatus.FAILED:
return containsAssertionError ? Status.FAILED : Status.BROKEN;
return error ? getStatusFromError(error) : Status.FAILED;
case TestStepResultStatus.PASSED:
return Status.PASSED;
case TestStepResultStatus.SKIPPED:
Expand Down Expand Up @@ -331,13 +330,21 @@ export default class AllureCucumberReporter extends Formatter {
}

this.allureRuntime.updateTest(testUuid, (result) => {
result.status = result.steps.length > 0 ? getWorstStepResultStatus(result.steps) : Status.PASSED;
const step = getWorstTestStepResult(result.steps);
result.status = step?.status ?? Status.PASSED;
result.stage = Stage.FINISHED;

if (result.status === undefined) {
result.statusDetails = {
message: "The test doesn't have an implementation.",
};
if (step) {
if (step.status === undefined) {
result.statusDetails = {
message: "The test doesn't have an implementation.",
};
} else {
result.statusDetails = {
...result.statusDetails,
...step.statusDetails,
};
}
}
});
this.allureRuntime.stopTest(testUuid, { stop: TimeConversion.timestampToMillisecondsSinceEpoch(data.timestamp) });
Expand Down Expand Up @@ -428,6 +435,7 @@ export default class AllureCucumberReporter extends Formatter {

const status = this.parseStatus(data.testStepResult);
const stage = status !== Status.SKIPPED ? Stage.FINISHED : Stage.PENDING;
const error = this.exceptionToError(data.testStepResult.message, data.testStepResult.exception);

if (step.hookId) {
const fixtureUuid = this.fixtureUuids.get(data.testCaseStartedId);
Expand All @@ -439,11 +447,11 @@ export default class AllureCucumberReporter extends Formatter {
r.stage = stage;
r.status = status;

if (data.testStepResult.exception) {
r.statusDetails = getMessageAndTraceFromError({
message: data.testStepResult.message,
stack: data.testStepResult.exception.stackTrace,
});
if (error) {
r.statusDetails = {
...r.statusDetails,
...getMessageAndTraceFromError(error),
};
}
});
this.allureRuntime.stopFixture(fixtureUuid, {
Expand Down Expand Up @@ -471,10 +479,10 @@ export default class AllureCucumberReporter extends Formatter {
return;
}

if (data.testStepResult.exception) {
if (error) {
r.statusDetails = {
message: data.testStepResult.message,
trace: data.testStepResult.exception.stackTrace,
...r.statusDetails,
...getMessageAndTraceFromError(error),
};
}
});
Expand Down Expand Up @@ -534,4 +542,15 @@ export default class AllureCucumberReporter extends Formatter {
this.allureRuntime.writeCategoriesDefinitions();
this.allureRuntime.writeEnvironmentInfo();
}

private exceptionToError(message?: string, exception?: messages.Exception): Error | undefined {
if (!exception) {
return;
}
return {
message: message ?? exception.message,
stack: exception.stackTrace,
name: exception.type,
} as Error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: errors

Scenario: failed
Given a step
11 changes: 11 additions & 0 deletions packages/allure-cucumberjs/test/samples/support/errors.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { Given } = require("@cucumber/cucumber");

Given("a step", async () => {
const error = new Error("some message");
error.stack =
" at Proxy.<anonymous> (node_modules/playwright/lib/matchers/expect.js:198:37)\n" +
" at Context.<anonymous> (test/spec/sample.js:6:13)\n" +
" at process.processImmediate (node:internal/timers:476:21)\n" +
" at process.callbackTrampoline (node:internal/async_hooks:130:17)";
throw error;
});
29 changes: 29 additions & 0 deletions packages/allure-cucumberjs/test/spec/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, it } from "vitest";
import { Status } from "allure-js-commons";
import { runCucumberInlineTest } from "../utils.js";

it("should support playwright expect", async () => {
const { tests } = await runCucumberInlineTest(["errors"], ["errors"]);

expect(tests).toHaveLength(1);
expect(tests).toContainEqual(
expect.objectContaining({
name: "failed",
status: Status.FAILED,
statusDetails: expect.objectContaining({
message: expect.stringContaining("some message"),
trace: expect.stringContaining("playwright/lib/matchers/expect.js"),
}),
steps: [
expect.objectContaining({
name: "Given a step",
status: Status.FAILED,
statusDetails: expect.objectContaining({
message: expect.stringContaining("some message"),
trace: expect.stringContaining("playwright/lib/matchers/expect.js"),
}),
}),
],
}),
);
});
45 changes: 10 additions & 35 deletions packages/allure-js-commons/src/sdk/reporter/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFile } from "fs/promises";
import { createHash, randomUUID } from "node:crypto";
import type { EventEmitter } from "node:events";
import fs from "node:fs";
import { readFile } from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import properties from "properties";
Expand Down Expand Up @@ -44,43 +44,18 @@ export const getTestResultTestCaseId = (result: TestResult) => {
return result.fullName ? md5(result.fullName) : undefined;
};

export const getWorstStepResultStatusPriority = (steps: StepResult[], priority?: number): number | undefined => {
let worstStatusPriority = priority;

steps.forEach((step) => {
if (step.steps?.length) {
worstStatusPriority = getWorstStepResultStatusPriority(step.steps, worstStatusPriority);
}

const stepStatusPriority = step.status ? StatusByPriority.indexOf(step.status) : undefined;

if (stepStatusPriority === undefined) {
return;
}

if (worstStatusPriority === undefined) {
worstStatusPriority = stepStatusPriority;
return;
}

if (stepStatusPriority >= worstStatusPriority) {
return;
}

worstStatusPriority = stepStatusPriority;
});

return worstStatusPriority;
const statusToPriority = (status: Status | undefined) => {
if (!status) {
return -1;
}
return StatusByPriority.indexOf(status);
};

export const getWorstStepResultStatus = (steps: StepResult[]): Status | undefined => {
const worstStatusPriority = getWorstStepResultStatusPriority(steps);

if (worstStatusPriority === undefined) {
return undefined;
export const getWorstTestStepResult = (steps: StepResult[]): StepResult | undefined => {
if (steps.length === 0) {
return;
}

return StatusByPriority[worstStatusPriority];
return [...steps].sort((a, b) => statusToPriority(a.status) - statusToPriority(b.status))[0];
};

export const readImageAsBase64 = async (filePath: string): Promise<string | undefined> => {
Expand Down
Loading