Skip to content

Commit

Permalink
Report parent suites with nesting correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Aug 23, 2019
1 parent 9aaa78f commit 350610a
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 34 deletions.
62 changes: 34 additions & 28 deletions packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ContentType,
LabelName,
Stage,
Status
Status,
StatusDetails
} from "allure-js-commons";
import { createHash } from "crypto";
import { MochaAllureInterface } from "./MochaAllureInterface";
Expand Down Expand Up @@ -45,11 +46,9 @@ export class AllureReporter {
}

public startSuite(suiteName: string) {
if (suiteName) {
const scope = this.currentSuite || this.runtime;
const suite = scope.startGroup(suiteName);
this.pushSuite(suite);
}
const scope = this.currentSuite || this.runtime;
const suite = scope.startGroup(suiteName || "Global");
this.pushSuite(suite);
}

public endSuite() {
Expand All @@ -62,39 +61,50 @@ export class AllureReporter {
}
}

public startCase(suiteName: string, testName: string) {
public startCase(test: Mocha.Test) {
if (this.currentSuite === null) {
throw new Error("No active suite");
}

this.currentTest = this.currentSuite.startTest(testName);
this.currentTest.fullName = testName;
this.currentTest = this.currentSuite.startTest(test.title);
this.currentTest.fullName = test.name;
this.currentTest.historyId = createHash("md5")
.update(JSON.stringify({ suite: suiteName, test: testName }))
.update(test.fullTitle())
.digest("hex");
this.currentTest.stage = Stage.RUNNING;
this.currentTest.addLabel(LabelName.SUITE, this.currentSuite.name);

if (test.parent) {
const [parentSuite, suite, ...subSuites] = test.parent.titlePath();
if (parentSuite) {
this.currentTest.addLabel(LabelName.PARENT_SUITE, parentSuite);
}
if (suite) {
this.currentTest.addLabel(LabelName.SUITE, suite);
}
if (subSuites.length > 0) {
this.currentTest.addLabel(LabelName.SUB_SUITE, subSuites.join(" > "));
}
}
}

public passTestCase() {
this.endTest();
this.endTest(Status.PASSED);
}

public pendingTestCase(test: Mocha.Test) {
if (this.currentTest === null && this.currentSuite !== null) {
this.currentTest = this.currentSuite.startTest(test.title);
this.currentTest.statusDetails = { message: "Test ignored" };
if (this.currentTest === null) {
this.startCase(test);
}

this.endTest(undefined, Status.SKIPPED);
this.endTest(Status.SKIPPED, { message: "Test ignored" });
}

public failTestCase(test: Mocha.Test, error: Error) {
if (this.currentTest === null && this.currentSuite !== null) {
this.currentTest = this.currentSuite.startTest(test.fullTitle());
if (this.currentTest === null) {
this.startCase(test);
}
const status = error.name === "AssertionError" ? Status.FAILED : Status.BROKEN;

this.endTest(error);
this.endTest(status, { message: error.message, trace: error.stack });
}

public writeAttachment(content: Buffer | string, type: ContentType): string {
Expand All @@ -117,19 +127,15 @@ export class AllureReporter {
this.suites.pop();
}

private endTest(error?: Error, status?: Status): void {
private endTest(status: Status, details?: StatusDetails): void {
if (this.currentTest === null) {
throw new Error("endTest while no test is running");
}

if (error) {
this.currentTest.statusDetails = { message: error.message, trace: error.stack };
if (details) {
this.currentTest.statusDetails = details;
}

let errorStatus = Status.PASSED;
if (error) errorStatus = error.name === "AssertionError" ? Status.FAILED : Status.BROKEN;

this.currentTest.status = status || errorStatus;
this.currentTest.status = status;
this.currentTest.stage = Stage.FINISHED;
this.currentTest.endTest();
this.currentTest = null;
Expand Down
5 changes: 2 additions & 3 deletions packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
}

private onTest(test: Mocha.Test) {
const suite = test.parent;
this.allure.startCase((suite && suite.title) || "Unnamed", test.title);
this.allure.startCase(test);
}

private onPassed(test: Mocha.Test) {
this.allure.passTestCase();
}

private onFailed(test: Mocha.Test, error: Error) {
console.error(error);
console.error("allure error", error);
this.allure.failTestCase(test, error);
}

Expand Down
25 changes: 25 additions & 0 deletions packages/allure-mocha/test/fixtures/specs/nested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from "chai";

it("top-level test", () => {
expect(1).to.be.eq(1);
});

describe("Parent suite", () => {
it("shallow test", () => {
expect(1).to.be.eq(1);
});

describe("Nested suite", () => {
describe("Sub suite", () => {
it("child test", () => {
expect(1).to.be.eq(1);
});

describe("Incredibly nested suite", () => {
it("the deepest test", () => {
expect(1).to.be.eq(1);
});
});
});
});
});
7 changes: 7 additions & 0 deletions packages/allure-mocha/test/fixtures/specs/pending.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("Pending tests", () => {
xit("simple pending", () => {});

it("skipped in runtime", function() {
this.skip();
});
});
17 changes: 15 additions & 2 deletions packages/allure-mocha/test/specs/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Status } from "allure-js-commons";
import { LabelName, Status } from "allure-js-commons";
import { expect } from "chai";
import { suite } from "mocha-typescript";
import { runTests } from "../utils";
import { findLabelValue, runTests } from "../utils";

@suite
class CommonSuite {
Expand All @@ -21,4 +21,17 @@ class CommonSuite {
expect(skippedTest.statusDetails.message).eq("Test ignored");
expect(skippedTest.statusDetails.trace).eq(undefined);
}

@test
async shouldHaveCorrectSuitesForPendingTests() {
const writerStub = await runTests("pending");

const simplePending = writerStub.getTestByName("simple pending");
expect(simplePending.status).eq(Status.SKIPPED);
expect(findLabelValue(simplePending, LabelName.PARENT_SUITE)).eq("Pending tests");

const skippedInRuntime = writerStub.getTestByName("skipped in runtime");
expect(skippedInRuntime.status).eq(Status.SKIPPED);
expect(findLabelValue(skippedInRuntime, LabelName.PARENT_SUITE)).eq("Pending tests");
}
}
45 changes: 45 additions & 0 deletions packages/allure-mocha/test/specs/nesting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { InMemoryAllureWriter, LabelName } from "allure-js-commons";
import { expect } from "chai";
import { suite } from "mocha-typescript";
import { findLabelValue, runTests } from "../utils";

@suite
class NestingSupport {
private writerStub!: InMemoryAllureWriter;

async before() {
this.writerStub = await runTests("nested");
}

@test
shouldAssignAllSuites() {
const test = this.writerStub.getTestByName("child test");
expect(findLabelValue(test, LabelName.PARENT_SUITE)).eq("Parent suite");
expect(findLabelValue(test, LabelName.SUITE)).eq("Nested suite");
expect(findLabelValue(test, LabelName.SUB_SUITE)).eq("Sub suite");
}

@test
shouldSkipMissingLevels() {
const test = this.writerStub.getTestByName("shallow test");
expect(findLabelValue(test, LabelName.PARENT_SUITE)).eq("Parent suite");
expect(findLabelValue(test, LabelName.SUITE)).eq(undefined);
expect(findLabelValue(test, LabelName.SUB_SUITE)).eq(undefined);
}

@test
shouldHandleTopLevelTests() {
const test = this.writerStub.getTestByName("top-level test");
expect(findLabelValue(test, LabelName.PARENT_SUITE)).eq(undefined);
expect(findLabelValue(test, LabelName.SUITE)).eq(undefined);
expect(findLabelValue(test, LabelName.SUB_SUITE)).eq(undefined);
}

@test
shouldMergeSubSuiteNames() {
const test = this.writerStub.getTestByName("the deepest test");
expect(findLabelValue(test, LabelName.PARENT_SUITE)).eq("Parent suite");
expect(findLabelValue(test, LabelName.SUITE)).eq("Nested suite");
expect(findLabelValue(test, LabelName.SUB_SUITE)).eq("Sub suite > Incredibly nested suite");
}
}
7 changes: 6 additions & 1 deletion packages/allure-mocha/test/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ function assignSpecs(mocha: Mocha, specs: string[]) {
jetpack
.dir(testDir)
.find({ matching: specs.map(spec => `${spec}.js`) })
.forEach(file => mocha.addFile(path.join(testDir, file)));
.forEach(file => {
const testPath = path.resolve(testDir, file);
// remove the test from node_modules cache, so it can be executed again
delete require.cache[testPath];
mocha.addFile(testPath);
});
}

0 comments on commit 350610a

Please sign in to comment.