Skip to content

Commit

Permalink
fix cypress runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Jul 4, 2024
1 parent f9a616c commit 35a7dac
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 49 deletions.
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

0 comments on commit 35a7dac

Please sign in to comment.