Skip to content

Commit

Permalink
feat: cypress report skipped tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBattaglia committed Jun 7, 2024
1 parent 6295a36 commit af151b5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
78 changes: 64 additions & 14 deletions packages/allure-cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,24 @@ export class AllureCypressTestRuntime implements TestRuntime {
this.sendMessage(message);
return Cypress.Promise.resolve();
}

sendSkippedTestMessages(messages: CypressRuntimeMessage[]) {
const skippedTestsMessages: CypressRuntimeMessage[][] | undefined = Cypress.env("skippedTestsMessages");

if (!skippedTestsMessages) {
Cypress.env("skippedTestsMessages", [messages]);
} else {
skippedTestsMessages.push(messages);
}
}
}

const { EVENT_TEST_BEGIN, EVENT_TEST_FAIL, EVENT_TEST_PASS } = Mocha.Runner.constants;
const {
EVENT_TEST_BEGIN,
EVENT_TEST_FAIL,
EVENT_TEST_PASS,
EVENT_TEST_PENDING
} = Mocha.Runner.constants;

const initializeAllure = () => {
const initialized = Cypress.env("allureInitialized") as boolean;
Expand Down Expand Up @@ -225,19 +240,46 @@ const initializeAllure = () => {
.on(EVENT_TEST_FAIL, (test: Mocha.Test, err: Error) => {
const testRuntime = getGlobalTestRuntime() as AllureCypressTestRuntime;

testRuntime.sendMessage({
type: "cypress_end",
data: {
stage: Stage.FINISHED,
status: err.constructor.name === "AssertionError" ? Status.FAILED : Status.BROKEN,
statusDetails: {
message: err.message,
trace: err.stack,
},
stop: Date.now(),
testRuntime.sendMessage({
type: "cypress_end",
data: {
stage: Stage.FINISHED,
status: err.constructor.name === "AssertionError" ? Status.FAILED : Status.BROKEN,
statusDetails: {
message: err.message,
trace: err.stack,
},
});
stop: Date.now(),
},
});
})
.on(EVENT_TEST_PENDING, (test: Mocha.Test, err: Error) => {
const testRuntime = new AllureCypressTestRuntime();

const startMessage: CypressRuntimeMessage = {
type: "cypress_start",
data: {
isInteractive: Cypress.config("isInteractive"),
absolutePath: Cypress.spec.absolute,
specPath: getSuitePath(test).concat(test.title),
filename: Cypress.spec.relative,
start: Date.now(),
},
};
const endMessage: CypressRuntimeMessage = {
type: "cypress_end",
data: {
stage: Stage.FINISHED,
status: Status.SKIPPED,
stop: Date.now(),
},
};

const skippedTestMessages: CypressRuntimeMessage[] = [startMessage, endMessage];
testRuntime.sendSkippedTestMessages(skippedTestMessages);

setGlobalTestRuntime(testRuntime);
});

Cypress.Screenshot.defaults({
onAfterScreenshot: (_, details) => {
Expand Down Expand Up @@ -290,8 +332,16 @@ const initializeAllure = () => {
throw err;
});

afterEach(() => {
const runtimeMessages = Cypress.env("allureRuntimeMessages") as CypressRuntimeMessage[];
after(() => {
const skippedTestsMessages = Cypress.env("skippedTestsMessages") as CypressRuntimeMessage[][];

for (const skippedTestMessages of skippedTestsMessages) {
cy.task("allureReportTest", skippedTestMessages, { log: false });
}
});

afterEach(() => {
const runtimeMessages = Cypress.env("allureRuntimeMessages") as CypressRuntimeMessage[];

cy.task("allureReportTest", runtimeMessages, { log: false });
});
Expand Down
26 changes: 26 additions & 0 deletions packages/allure-cypress/test/spec/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,29 @@ it("broken test", async () => {
expect(tests[0].stage).toBe(Stage.FINISHED);
expect(tests[0].statusDetails).toHaveProperty("message", "broken");
});

it("skipped tests", async () => {
const { tests } = await runCypressInlineTest(
() => `
it.skip("skipped-1", () => {
cy.wrap(1).should("eq", 1);
});
it("passing", () => {
cy.wrap(1).should("eq", 1);
});
it.skip("skipped-2", () => {
cy.wrap(2).should("eq", 2);
});
`,
);

expect(tests).toHaveLength(3);
// The passing test is first, because afterEach hook runs before after hook
expect(tests[0].status).toBe(Status.PASSING);
expect(tests[0].stage).toBe(Stage.FINISHED);

expect(tests[1].status).toBe(Status.SKIPPED);
expect(tests[1].stage).toBe(Stage.FINISHED);
expect(tests[2].status).toBe(Status.SKIPPED);
expect(tests[2].stage).toBe(Stage.FINISHED);
});

0 comments on commit af151b5

Please sign in to comment.