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

feat(commons): add logStep Runtime API #1043

Merged
merged 2 commits into from
Jul 4, 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
28 changes: 27 additions & 1 deletion packages/allure-cypress/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AttachmentOptions, Label, Link, ParameterMode, ParameterOptions, StatusDetails } from "allure-js-commons";
import { ContentType, Status } from "allure-js-commons";
import type { RuntimeMessage, TestPlanV1 } from "allure-js-commons/sdk";
import { getUnfinishedStepsMessages, hasStepMessage } from "allure-js-commons/sdk";
import { getMessageAndTraceFromError, getUnfinishedStepsMessages, hasStepMessage } from "allure-js-commons/sdk";
import type { TestRuntime } from "allure-js-commons/sdk/runtime";
import { getGlobalTestRuntime, setGlobalTestRuntime } from "allure-js-commons/sdk/runtime";
import type {
Expand Down Expand Up @@ -134,6 +134,32 @@ export class AllureCypressTestRuntime implements TestRuntime {
});
}

logStep(name: string, status: Status = Status.PASSED, error?: Error) {
return cy
.wrap(ALLURE_REPORT_STEP_COMMAND, { log: false })
.then(() => {
this.sendMessage({
type: "step_start",
data: {
name,
start: Date.now(),
},
});

return Cypress.Promise.resolve();
})
.then(() => {
return this.sendMessageAsync({
type: "step_stop",
data: {
status: status,
stop: Date.now(),
statusDetails: error ? { ...getMessageAndTraceFromError(error) } : undefined,
},
});
});
}

step<T = void>(name: string, body: () => T | PromiseLike<T>) {
return cy
.wrap(ALLURE_REPORT_STEP_COMMAND, { log: false })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("text", async () => {
const { tests, attachments } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { attachment } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { attachment } from "${allureCommonsModulePath}";

it("text attachment", () => {
attachment("foo.txt", "bar", "text/plain");
Expand All @@ -24,8 +24,8 @@ it("text", async () => {

it("json", async () => {
const { tests, attachments } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { attachment } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { attachment } from "${allureCommonsModulePath}";

it("json attachment", () => {
attachment("foo", JSON.stringify({ foo: "bar" }), "application/json");
Expand All @@ -46,8 +46,8 @@ it("json", async () => {

it("cypress read file", async () => {
const { tests, attachments } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { attachment } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { attachment } from "${allureCommonsModulePath}";

it("json attachment", () => {
cy.readFile("foo.txt", null).then((content) => {
Expand All @@ -69,8 +69,8 @@ it("cypress read file", async () => {

it("handles allure attachments inside cypress hooks", async () => {
const { tests, groups } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { attachment } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { attachment } from "${allureCommonsModulePath}";

describe("suite", () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("description", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { description } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { description } from "${allureCommonsModulePath}";

it("markdown", () => {
description("foo");
Expand All @@ -18,8 +18,8 @@ it("description", async () => {

it("descriptionHtml", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { descriptionHtml } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { descriptionHtml } from "${allureCommonsModulePath}";

it("html", () => {
descriptionHtml("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("displayName", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { displayName } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { displayName } from "${allureCommonsModulePath}";

it("sample", () => {
displayName("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("historyId", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { historyId } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { historyId } from "${allureCommonsModulePath}";

it("sample", () => {
historyId("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { runCypressInlineTest } from "../../../utils.js";

it("adds all the possible labels", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import {
label,
allureId,
Expand All @@ -18,7 +18,7 @@ it("adds all the possible labels", async () => {
severity,
story,
tag,
} from "${allureCypressModulePath}";
} from "${allureCommonsModulePath}";

it("labels", () => {
label("foo", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("adds all the possible links", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { link, issue, tms } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { link, issue, tms } from "${allureCommonsModulePath}";

it("links", () => {
link("https://allurereport.org", "foo", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("parameters", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { parameter } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { parameter } from "${allureCommonsModulePath}";

it("adds parameter", () => {
parameter("foo", "bar", {
Expand Down
60 changes: 38 additions & 22 deletions packages/allure-cypress/test/spec/runtime/modern/steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("single step", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { label, step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { label, step } from "${allureCommonsModulePath}";

it("step", () => {
step("foo", () => {
Expand All @@ -23,8 +23,8 @@ it("single step", async () => {

it("multiple steps", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { label, step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { label, step } from "${allureCommonsModulePath}";

it("step", () => {
step("foo", () => {
Expand Down Expand Up @@ -54,8 +54,8 @@ it("multiple steps", async () => {

it("nested steps", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { label, step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { label, step } from "${allureCommonsModulePath}";

it("step", () => {
step("foo", () => {
Expand All @@ -81,8 +81,8 @@ it("nested steps", async () => {

it("step with attachments", async () => {
const { tests, attachments } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { attachment, step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { attachment, step } from "${allureCommonsModulePath}";

it("text attachment", () => {
step("foo", () => {
Expand All @@ -105,8 +105,8 @@ it("step with attachments", async () => {

it("step with screenshot", async () => {
const { tests, attachments } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step } from "${allureCommonsModulePath}";

it("manual", () => {
step("foo", () => {
Expand Down Expand Up @@ -144,8 +144,8 @@ it("step with screenshot", async () => {

it("step with cypress assertion error", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step } from "${allureCommonsModulePath}";

it("step", () => {
step("foo", () => {
Expand All @@ -168,8 +168,8 @@ it("step with cypress assertion error", async () => {

it("step with unexpected error", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step } from "${allureCommonsModulePath}";

it("step", () => {
step("foo", () => {
Expand All @@ -192,11 +192,11 @@ it("step with unexpected error", async () => {

it("step runtime api", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step } from "${allureCommonsModulePath}";

it("step", () => {
step("${allureCypressModulePath}", (ctx) => {
step("${allureCommonsModulePath}", (ctx) => {
ctx.displayName("bar");
ctx.parameter("p1", "v1");
ctx.parameter("p2", "v2", "default");
Expand Down Expand Up @@ -224,12 +224,12 @@ it("step runtime api", async () => {

it("promise-step", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step, label } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step, label } from "${allureCommonsModulePath}";

it("step", () => {
let value = "unset";
step("${allureCypressModulePath}", () => {
step("${allureCommonsModulePath}", () => {
return new Cypress.Promise(
(r) => setTimeout(() => {
value = "set";
Expand All @@ -252,8 +252,8 @@ it("promise-step", async () => {

it("handles allure steps inside cypress hooks", async () => {
const { tests, groups } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { step } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { step } from "${allureCommonsModulePath}";

describe("suite", () => {
beforeEach(() => {
Expand Down Expand Up @@ -294,3 +294,19 @@ it("handles allure steps inside cypress hooks", async () => {
]),
);
});

it("log step", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { label, logStep, Status } from "${allureCommonsModulePath}";

it("step", () => {
logStep("log step hello", Status.FAILED);
});
`,
});

expect(tests).toHaveLength(1);
expect(tests[0].steps).toHaveLength(1);
expect(tests[0].steps).toContainEqual(expect.objectContaining({ name: "log step hello", status: Status.FAILED }));
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("testCaseId", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { testCaseId } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { testCaseId } from "${allureCommonsModulePath}";

it("sample", () => {
testCaseId("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { runCypressInlineTest } from "../../../utils.js";

it("title metadata", async () => {
const { tests } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": ({ allureCypressModulePath }) => `
import { label } from "${allureCypressModulePath}";
"cypress/e2e/sample.cy.js": ({ allureCommonsModulePath }) => `
import { label } from "${allureCommonsModulePath}";

it("foo @allure.id=1 @allure.label.foo=2", () => {
label("bar", "3");
Expand Down
7 changes: 6 additions & 1 deletion packages/allure-js-commons/src/facade.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ContentType } from "./model.js";
import type { Status } from "./model.js";
import { type ContentType } from "./model.js";
import { type AttachmentOptions, type Label, type Link, type ParameterMode, type ParameterOptions } from "./model.js";
import { LabelName, LinkType } from "./model.js";
import { getGlobalTestRuntimeWithAutoconfig } from "./sdk/runtime/runtime.js";
Expand Down Expand Up @@ -98,6 +99,10 @@ const stepContext: () => StepContext = () => ({
},
});

export const logStep = (name: string, status?: Status, error?: Error): PromiseLike<void> => {
return callRuntimeMethod("logStep", name, status, error);
};

export const step = <T = void>(name: string, body: (context: StepContext) => T | PromiseLike<T>): PromiseLike<T> => {
return callRuntimeMethod("step", name, () => body(stepContext()));
};
Expand Down
1 change: 1 addition & 0 deletions packages/allure-js-commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
layer,
link,
links,
logStep,
owner,
parameter,
parentSuite,
Expand Down
Loading
Loading