From 2d052723c8086199d0e1b9802cc6e535c7cc5990 Mon Sep 17 00:00:00 2001 From: epszaw Date: Thu, 17 Aug 2023 11:42:04 +0200 Subject: [PATCH] fix errors reporting for steps fix skipped tests reporting cover rest functionality with tests hide jest log during unit tests --- packages/allure-jest/src/AllureJest.ts | 10 +++- .../test/fixtures/attachments.test.js | 3 ++ .../test/fixtures/description.test.js | 7 +++ .../test/fixtures/historyId.test.js | 3 ++ .../test/fixtures/parameters.test.js | 6 +++ .../allure-jest/test/fixtures/skipped.test.js | 5 ++ .../allure-jest/test/fixtures/steps.test.js | 19 +++++++ .../test/fixtures/testCaseId.test.js | 3 ++ .../allure-jest/test/fixtures/todo.test.js | 1 + .../allure-jest/test/spec/attachments.test.ts | 19 +++++++ .../allure-jest/test/spec/description.test.ts | 22 ++++++++ .../allure-jest/test/spec/historyId.test.ts | 16 ++++++ .../allure-jest/test/spec/parameters.test.ts | 20 ++++++++ .../allure-jest/test/spec/skipped.test.ts | 25 ++++++++++ packages/allure-jest/test/spec/steps.test.ts | 50 +++++++++++++++++++ .../allure-jest/test/spec/testCaseId.test.ts | 16 ++++++ packages/allure-jest/test/spec/todo.test.ts | 18 +++++++ packages/allure-jest/test/utils.ts | 3 +- 18 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 packages/allure-jest/test/fixtures/attachments.test.js create mode 100644 packages/allure-jest/test/fixtures/description.test.js create mode 100644 packages/allure-jest/test/fixtures/historyId.test.js create mode 100644 packages/allure-jest/test/fixtures/parameters.test.js create mode 100644 packages/allure-jest/test/fixtures/skipped.test.js create mode 100644 packages/allure-jest/test/fixtures/steps.test.js create mode 100644 packages/allure-jest/test/fixtures/testCaseId.test.js create mode 100644 packages/allure-jest/test/fixtures/todo.test.js create mode 100644 packages/allure-jest/test/spec/attachments.test.ts create mode 100644 packages/allure-jest/test/spec/description.test.ts create mode 100644 packages/allure-jest/test/spec/historyId.test.ts create mode 100644 packages/allure-jest/test/spec/parameters.test.ts create mode 100644 packages/allure-jest/test/spec/skipped.test.ts create mode 100644 packages/allure-jest/test/spec/steps.test.ts create mode 100644 packages/allure-jest/test/spec/testCaseId.test.ts create mode 100644 packages/allure-jest/test/spec/todo.test.ts diff --git a/packages/allure-jest/src/AllureJest.ts b/packages/allure-jest/src/AllureJest.ts index a172f5cb8..39a65f1a2 100644 --- a/packages/allure-jest/src/AllureJest.ts +++ b/packages/allure-jest/src/AllureJest.ts @@ -130,12 +130,15 @@ export default class AllureJest extends NodeEnvironment { private handleTestFail(test: Circus.TestEntry) { const currentTestID = getTestID(getTestPath(test)); const currentTest = this.runningTests.get(currentTestID)!; + // jest collects all errors, but we need to report the first one because it's a reason why the test has been failed + const [error] = test.errors; + const hasMultipleErrors = Array.isArray(error); currentTest.stage = Stage.FINISHED; currentTest.status = Status.FAILED; currentTest.statusDetails = { - message: test.errors[0].message, - trace: test.errors[0].stack, + message: hasMultipleErrors ? error[0].message : error.message, + trace: hasMultipleErrors ? error[0].stack : error.stack, }; } @@ -145,6 +148,9 @@ export default class AllureJest extends NodeEnvironment { currentTest.stage = Stage.PENDING; currentTest.status = Status.SKIPPED; + + currentTest.endTest(); + this.runningTests.delete(currentTestID); } private handleTestDone(test: Circus.TestEntry) { diff --git a/packages/allure-jest/test/fixtures/attachments.test.js b/packages/allure-jest/test/fixtures/attachments.test.js new file mode 100644 index 000000000..55ae9361b --- /dev/null +++ b/packages/allure-jest/test/fixtures/attachments.test.js @@ -0,0 +1,3 @@ +it("json", () => { + allure.attachment(JSON.stringify({ foo: "bar" }), "application/json"); +}); diff --git a/packages/allure-jest/test/fixtures/description.test.js b/packages/allure-jest/test/fixtures/description.test.js new file mode 100644 index 000000000..80fa5edbe --- /dev/null +++ b/packages/allure-jest/test/fixtures/description.test.js @@ -0,0 +1,7 @@ +it("markdown", () => { + allure.description("foo"); +}); + +it("html", () => { + allure.descriptionHtml("foo"); +}); diff --git a/packages/allure-jest/test/fixtures/historyId.test.js b/packages/allure-jest/test/fixtures/historyId.test.js new file mode 100644 index 000000000..b093c7f30 --- /dev/null +++ b/packages/allure-jest/test/fixtures/historyId.test.js @@ -0,0 +1,3 @@ +it("historyId", () => { + allure.historyId("foo"); +}); diff --git a/packages/allure-jest/test/fixtures/parameters.test.js b/packages/allure-jest/test/fixtures/parameters.test.js new file mode 100644 index 000000000..1be39e63e --- /dev/null +++ b/packages/allure-jest/test/fixtures/parameters.test.js @@ -0,0 +1,6 @@ +it("custom", () => { + allure.parameter("foo", "bar", { + excluded: false, + mode: "hidden", + }); +}); diff --git a/packages/allure-jest/test/fixtures/skipped.test.js b/packages/allure-jest/test/fixtures/skipped.test.js new file mode 100644 index 000000000..710f2af7e --- /dev/null +++ b/packages/allure-jest/test/fixtures/skipped.test.js @@ -0,0 +1,5 @@ +it.skip("skipped", () => {}); + +describe.skip("suite", () => { + it("skipped", () => {}); +}); diff --git a/packages/allure-jest/test/fixtures/steps.test.js b/packages/allure-jest/test/fixtures/steps.test.js new file mode 100644 index 000000000..52cfa5ce6 --- /dev/null +++ b/packages/allure-jest/test/fixtures/steps.test.js @@ -0,0 +1,19 @@ +it("passed", async () => { + await allure.step("first step name", async (s1) => { + await s1.step("second step name", async (s2) => { + await s2.step("third step name", (s3) => { + s3.label("foo", "bar"); + }); + }); + }); +}); + +it("failed", async () => { + await allure.step("first step name", async (s1) => { + await s1.step("second step name", async (s2) => { + await s2.step("third step name", (s3) => { + throw new Error("foo"); + }); + }); + }); +}); diff --git a/packages/allure-jest/test/fixtures/testCaseId.test.js b/packages/allure-jest/test/fixtures/testCaseId.test.js new file mode 100644 index 000000000..998fd6b19 --- /dev/null +++ b/packages/allure-jest/test/fixtures/testCaseId.test.js @@ -0,0 +1,3 @@ +it("testCaseId", () => { + allure.testCaseId("foo"); +}); diff --git a/packages/allure-jest/test/fixtures/todo.test.js b/packages/allure-jest/test/fixtures/todo.test.js new file mode 100644 index 000000000..07dc494bc --- /dev/null +++ b/packages/allure-jest/test/fixtures/todo.test.js @@ -0,0 +1 @@ +it.todo("todo"); diff --git a/packages/allure-jest/test/spec/attachments.test.ts b/packages/allure-jest/test/spec/attachments.test.ts new file mode 100644 index 000000000..7adeca1f4 --- /dev/null +++ b/packages/allure-jest/test/spec/attachments.test.ts @@ -0,0 +1,19 @@ +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("attachments", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/attachments.test.js"]); + }); + + it("adds markdown description", () => { + const { attachments } = results.json; + + attachments.should.include.something.like({ + name: "Attachment", + type: "application/json", + }); + }); +}); diff --git a/packages/allure-jest/test/spec/description.test.ts b/packages/allure-jest/test/spec/description.test.ts new file mode 100644 index 000000000..7a836862a --- /dev/null +++ b/packages/allure-jest/test/spec/description.test.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("description", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/description.test.js"]); + }); + + it("adds markdown description", () => { + const { description } = results.markdown; + + expect(description).eq("foo"); + }); + + it("adds custom history id", () => { + const { descriptionHtml } = results.html; + + expect(descriptionHtml).eq("foo"); + }); +}); diff --git a/packages/allure-jest/test/spec/historyId.test.ts b/packages/allure-jest/test/spec/historyId.test.ts new file mode 100644 index 000000000..f09a28e40 --- /dev/null +++ b/packages/allure-jest/test/spec/historyId.test.ts @@ -0,0 +1,16 @@ +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("historyId", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/historyId.test.js"]); + }); + + it("adds custom history id", () => { + const { historyId } = results.historyId; + + expect(historyId).eq("foo"); + }); +}); diff --git a/packages/allure-jest/test/spec/parameters.test.ts b/packages/allure-jest/test/spec/parameters.test.ts new file mode 100644 index 000000000..7567b2fd4 --- /dev/null +++ b/packages/allure-jest/test/spec/parameters.test.ts @@ -0,0 +1,20 @@ +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("parameters", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/parameters.test.js"]); + }); + + it("adds custom parameter", () => { + const { parameters } = results.custom; + + parameters.should.include.something.that.deep.equals({ + name: "foo", + value: "bar", + excluded: false, + mode: "hidden", + }); + }); +}); diff --git a/packages/allure-jest/test/spec/skipped.test.ts b/packages/allure-jest/test/spec/skipped.test.ts new file mode 100644 index 000000000..91bbafa73 --- /dev/null +++ b/packages/allure-jest/test/spec/skipped.test.ts @@ -0,0 +1,25 @@ +import { Stage, Status } from "allure-js-commons"; +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("skipped", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/skipped.test.js"]); + }); + + it("marks skipped test as skipped", () => { + const { stage, status } = results.skipped; + + expect(stage).eq(Stage.PENDING); + expect(status).eq(Status.SKIPPED); + }); + + it("marks test inside skipped suite as skipped", () => { + const { stage, status } = results["suite skipped"]; + + expect(stage).eq(Stage.PENDING); + expect(status).eq(Status.SKIPPED); + }); +}); diff --git a/packages/allure-jest/test/spec/steps.test.ts b/packages/allure-jest/test/spec/steps.test.ts new file mode 100644 index 000000000..e61603763 --- /dev/null +++ b/packages/allure-jest/test/spec/steps.test.ts @@ -0,0 +1,50 @@ +import { Stage, Status } from "allure-js-commons"; +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("steps", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/steps.test.js"]); + }); + + describe("passed steps", () => { + it("adds nested steps", () => { + const { steps, labels } = results.passed; + + labels.should.include.something.that.deep.equals({ + name: "foo", + value: "bar", + }); + + expect(steps.length).eq(1); + expect(steps[0].name).eq("first step name"); + expect(steps[0].steps.length).eq(1); + expect(steps[0].steps[0].name).eq("second step name"); + expect(steps[0].steps[0].steps.length).eq(1); + expect(steps[0].steps[0].steps[0].name).eq("third step name"); + }); + }); + + describe("failed steps", () => { + it("fails the test with original step error", () => { + const { status, statusDetails, steps } = results.failed; + + expect(status).eq(Status.FAILED); + expect(statusDetails.message).eq("foo"); + expect(steps).to.have.length(1); + expect(steps[0].name).eq("first step name"); + expect(steps[0].status).eq(Status.BROKEN); + expect(steps[0].statusDetails.message).eq("foo"); + expect(steps[0].steps.length).eq(1); + expect(steps[0].steps[0].name).eq("second step name"); + expect(steps[0].steps[0].status).eq(Status.BROKEN); + expect(steps[0].steps[0].statusDetails.message).eq("foo"); + expect(steps[0].steps[0].steps.length).eq(1); + expect(steps[0].steps[0].steps[0].name).eq("third step name"); + expect(steps[0].steps[0].steps[0].status).eq(Status.BROKEN); + expect(steps[0].steps[0].steps[0].statusDetails.message).eq("foo"); + }); + }); +}); diff --git a/packages/allure-jest/test/spec/testCaseId.test.ts b/packages/allure-jest/test/spec/testCaseId.test.ts new file mode 100644 index 000000000..2ef0e89dc --- /dev/null +++ b/packages/allure-jest/test/spec/testCaseId.test.ts @@ -0,0 +1,16 @@ +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("testCaseId", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/testCaseId.test.js"]); + }); + + it("adds custom history id", () => { + const { testCaseId } = results.testCaseId; + + expect(testCaseId).eq("foo"); + }); +}); diff --git a/packages/allure-jest/test/spec/todo.test.ts b/packages/allure-jest/test/spec/todo.test.ts new file mode 100644 index 000000000..71d47027e --- /dev/null +++ b/packages/allure-jest/test/spec/todo.test.ts @@ -0,0 +1,18 @@ +import { Stage, Status } from "allure-js-commons"; +import { expect } from "chai"; +import { runJestTests, TestResultsByFullName } from "../utils"; + +describe("todo", () => { + let results: TestResultsByFullName; + + beforeEach(async () => { + results = await runJestTests(["./test/fixtures/todo.test.js"]); + }); + + it("marks todo tests as skipped", () => { + const { stage, status } = results.todo; + + expect(stage).eq(Stage.PENDING); + expect(status).eq(Status.SKIPPED); + }); +}); diff --git a/packages/allure-jest/test/utils.ts b/packages/allure-jest/test/utils.ts index b8a3380da..32e0dc666 100644 --- a/packages/allure-jest/test/utils.ts +++ b/packages/allure-jest/test/utils.ts @@ -24,8 +24,7 @@ export const runJestTests = async (fixtures: string[]): Promise