Skip to content

Commit

Permalink
Correctly report information from before/after hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Aug 26, 2019
1 parent ab47cdd commit c933122
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/allure-js-commons/src/ExecutableItemWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class ExecutableItemWrapper {
this.info.status = status;
}

public get status() {
return this.info.status;
}

public set statusDetails(details: StatusDetails) {
this.info.statusDetails = details;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/allure-js-commons/src/constructors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { FixtureResult, StepResult, TestResult, TestResultContainer } from "./model";
import { FixtureResult, Stage, Status, StepResult, TestResult, TestResultContainer } from "./model";
import { v4 as randomUUID } from "uuid";
import { Status } from "./model";
import { Stage } from "./model";

export function testResultContainer(): TestResultContainer {
return {
Expand All @@ -25,7 +23,7 @@ export function fixtureResult(): FixtureResult {

export function stepResult(): StepResult {
return {
status: Status.BROKEN,
status: Status.PASSED,
statusDetails: {},
stage: Stage.PENDING,
steps: [],
Expand All @@ -38,7 +36,7 @@ export function testResult(): TestResult {
return {
uuid: randomUUID(),
historyId: randomUUID(),
status: Status.BROKEN,
status: Status.PASSED,
statusDetails: {},
stage: Stage.PENDING,
steps: [],
Expand Down
16 changes: 11 additions & 5 deletions packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,27 @@ export class AllureReporter {
}
}

public passTestCase() {
public passTestCase(test: Mocha.Test) {
if (this.currentTest === null) {
this.startCase(test);
}
this.endTest(Status.PASSED);
}

public pendingTestCase(test: Mocha.Test) {
if (this.currentTest === null) {
this.startCase(test);
}
this.startCase(test);
this.endTest(Status.SKIPPED, { message: "Test ignored" });
}

public failTestCase(test: Mocha.Test, error: Error) {
if (this.currentTest === null) {
this.startCase(test);
} else {
const latestStatus = this.currentTest.status;
// if test already has a failed state, we should not overwrite it
if (latestStatus === Status.FAILED || latestStatus === Status.BROKEN) {
return;
}
}
const status = error.name === "AssertionError" ? Status.FAILED : Status.BROKEN;

Expand Down Expand Up @@ -138,6 +145,5 @@ export class AllureReporter {
this.currentTest.status = status;
this.currentTest.stage = Stage.FINISHED;
this.currentTest.endTest();
this.currentTest = null;
}
}
2 changes: 1 addition & 1 deletion packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
}

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

private onFailed(test: Mocha.Test, error: Error) {
Expand Down
54 changes: 54 additions & 0 deletions packages/allure-mocha/test/fixtures/specs/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { MochaAllureInterface } from "../../..";
import { ContentType } from "allure-js-commons";
import { expect } from "chai";

declare const allure: MochaAllureInterface;

describe("hooks test", () => {
describe("before fails", () => {
before(function() {
throw new Error("In before");
});

it("never runs", () => {});
});

describe("after fails", () => {
it("fails in after", () => {});

after(function() {
throw new Error("In after");
});
});

describe("beforeEach fails", () => {
beforeEach(function() {
allure.attachment("saved in beforeEach", "should be saved", ContentType.TEXT);
throw new Error("In before each");
});

it("test with beforeEach", () => {});
});

describe("afterEach fails", () => {
afterEach(function() {
allure.attachment("saved in afterEach", "should be saved", ContentType.TEXT);
throw new Error("In after each");
});

it("passed test with afterEach", () => {});
});

describe("both afterEach and test fail", () => {
afterEach(function() {
allure.attachment("saved in afterEach", "should be saved", ContentType.TEXT);
throw new Error("In after each");
});

it("failed test with afterEach", () => {
expect(1).eq(2);
});
});
});


59 changes: 59 additions & 0 deletions packages/allure-mocha/test/specs/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { InMemoryAllureWriter, Status } from "allure-js-commons";
import { expect } from "chai";
import { suite } from "mocha-typescript";
import { runTests } from "../utils";

@suite
class HooksSuite {
private writerStub!: InMemoryAllureWriter;

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

@test
shouldHandleBeforeEach() {
const test = this.writerStub.getTestByName("test with beforeEach");

expect(test.status).eq(Status.BROKEN);
expect(test.statusDetails.message).eq("In before each");
expect(test.attachments).have.length(1);
expect(test.attachments[0].name).eq("saved in beforeEach");
}

@test
shouldHandleAfterEach() {
const test = this.writerStub.getTestByName("passed test with afterEach");

expect(test.status).eq(Status.BROKEN);
expect(test.statusDetails.message).eq("In after each");
expect(test.attachments).have.length(1);
expect(test.attachments[0].name).eq("saved in afterEach");
}

@test
shouldPreserveTestErrorIfAfterEachFails() {
const test = this.writerStub.getTestByName("failed test with afterEach");

expect(test.status).eq(Status.FAILED);
expect(test.statusDetails.message).eq("expected 1 to equal 2");
expect(test.attachments).have.length(1);
expect(test.attachments[0].name).eq("saved in afterEach");
}

@test
shouldHandleBefore() {
const test = this.writerStub.getTestByName("\"before all\" hook for \"never runs\"");

expect(test.status).eq(Status.BROKEN);
expect(test.statusDetails.message).eq("In before");
}

@test
shouldHandleAfter() {
const test = this.writerStub.getTestByName("fails in after");

expect(test.status).eq(Status.BROKEN);
expect(test.statusDetails.message).eq("In after");
}
}

0 comments on commit c933122

Please sign in to comment.