Skip to content

Commit

Permalink
Support reporting errors in afterAll.
Browse files Browse the repository at this point in the history
Starting in jasmine 2.5, errors in afterAll are reported to jasmineDone or suiteDone.
Add ExecutionMetrics.errors to store these errors; report them.

Fixes #210
  • Loading branch information
johnjbarton committed Jul 29, 2017
1 parent 4a5506e commit ddfd728
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
43 changes: 43 additions & 0 deletions spec/unit/default-display.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,49 @@ describe("with default display", () => {
);
});

it("should report ERRORS when afterAll in suite throws", done => {
JasmineEnv.execute(
this.reporter,
env => {
env.describe("suite", () => {
env.it("spec", () => {
env.passed();
});
env.afterAll(() => {
throw new Error("afterAll threw");
});
});
},
(outputs, summary) => {
expect(summary).toContain("Executed 1 of 1 spec (1 ERROR) in {time}.");
done();
}
);
});

it("should report ERRORS when afterAll throws", done => {
JasmineEnv.execute(
this.reporter,
env => {
env.describe("suite", () => {
env.it("spec", () => {
env.passed();
});
env.afterAll(() => {
throw new Error("afterAll in suite threw");
});
});
env.afterAll(() => {
throw new Error("afterAll threw");
});
},
(outputs, summary) => {
expect(summary).toContain("Executed 1 of 1 spec (2 ERRORS) in {time}.");
done();
}
);
});

it("should report skipped with failure and pending", done => {
JasmineEnv.execute(
this.reporter,
Expand Down
44 changes: 44 additions & 0 deletions spec/unit/display-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,50 @@ describe("spec reporter", () => {
});
});

describe("when suite done", () => {
it("should report errors in afterAll", done => {
JasmineEnv.execute(
this.reporter,
env => {
env.describe("suite", () => {
env.it("passing spec", () => {
env.passed();
});
env.afterAll(() => {
throw new Error("throw in afterAll in suite");
});
});
},
outputs => {
expect(outputs).contains(/throw in afterAll/);
done();
}
);
});
});

describe("when jasmine done", () => {
it("should report errors in afterAll", done => {
JasmineEnv.execute(
this.reporter,
env => {
env.describe("suite", () => {
env.it("passing spec", () => {
env.passed();
});
});
env.afterAll(() => {
throw new Error("throw in afterAll");
});
},
outputs => {
expect(outputs).contains(/throw in afterAll/);
done();
}
);
});
});

describe("when summary", () => {
it("should display summary error messages", done => {
JasmineEnv.execute(
Expand Down
5 changes: 4 additions & 1 deletion src/display/execution-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export class ExecutionDisplay {
this.suiteHierarchy.push(result);
}

public suiteDone(): void {
public suiteDone(result: CustomReporterResult): void {
if (result && result.failedExpectations && result.failedExpectations.length) {
this.failed(result);
}
const suite: CustomReporterResult = this.suiteHierarchy.pop();
if (this.suiteHierarchyDisplayed[this.suiteHierarchyDisplayed.length - 1] === suite) {
this.suiteHierarchyDisplayed.pop();
Expand Down
38 changes: 36 additions & 2 deletions src/display/summary-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export class SummaryDisplay {
const pluralizedSpec = (metrics.totalSpecsDefined === 1 ? " spec" : " specs");
const execution = `Executed ${metrics.executedSpecs} of ${metrics.totalSpecsDefined}${pluralizedSpec}`;
let status = "";
if (metrics.failedSpecs === 0) {
if (metrics.failedSpecs === 0 && metrics.errors.length === 0) {
status = (metrics.totalSpecsDefined === metrics.executedSpecs) ?
" SUCCESS".successful : " INCOMPLETE".pending;
}
const failed = (metrics.failedSpecs > 0) ? ` (${metrics.failedSpecs} FAILED)` : "";
const pending = (metrics.pendingSpecs > 0) ? ` (${metrics.pendingSpecs} PENDING)` : "";
const skipped = (metrics.skippedSpecs > 0) ? ` (${metrics.skippedSpecs} SKIPPED)` : "";
let errors = (metrics.errors.length > 1) ? ` (${metrics.errors.length} ERRORS)` : "";
errors = (metrics.errors.length === 1) ? ` (${metrics.errors.length} ERROR)` : errors;
const duration = this.configuration.summary.displayDuration ? ` in ${metrics.duration}` : "";

this.logger.resetIndent();
Expand All @@ -29,10 +31,14 @@ export class SummaryDisplay {
if (this.configuration.summary.displayFailed && metrics.failedSpecs > 0) {
this.failuresSummary();
}
if (this.configuration.summary.displayFailed && metrics.errors.length > 0) {
this.errorsSummary(metrics.errors);
}
if (this.configuration.summary.displayPending && metrics.pendingSpecs > 0) {
this.pendingsSummary();
}
this.logger.log(execution + status + failed.failed + pending.pending + skipped.pending + duration + ".");
this.logger.log(execution + status + errors.failed + failed.failed
+ pending.pending + skipped.pending + duration + ".");

if (metrics.random) {
this.logger.log(`Randomized with seed ${metrics.seed}.`);
Expand Down Expand Up @@ -103,4 +109,32 @@ export class SummaryDisplay {
this.logger.log(pendingReason.pending);
this.logger.resetIndent();
}

private errorsSummary(errors: CustomReporterResult[]): void {
this.logger.log("**************************************************");
this.logger.log("* Errors *");
this.logger.log("**************************************************");
this.logger.newLine();
for (let i = 0; i < errors.length; i++) {
this.errorSummary(errors[i], i + 1);
this.logger.newLine();
}
this.logger.newLine();
this.logger.resetIndent();
}

private errorSummary(error: CustomReporterResult, index: number): void {
this.logger.log(`${index}) ${error.fullName}`);
if (this.configuration.summary.displayErrorMessages) {
this.logger.increaseIndent();
this.logger.process(
error,
(displayProcessor: DisplayProcessor, object: CustomReporterResult, log: string) => {
return displayProcessor.displaySummaryErrorMessages(object, log);
}
);
this.logger.decreaseIndent();
}
}

}
1 change: 1 addition & 0 deletions src/execution-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ExecutionMetrics {
public skippedSpecs = 0;
public totalSpecsDefined = 0;
public executedSpecs = 0;
public errors: CustomReporterResult[] = [];
public duration: string;
public random = false;
public seed: string;
Expand Down
29 changes: 28 additions & 1 deletion src/spec-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export class SpecReporter implements CustomReporter {

public jasmineDone(runDetails: RunDetails): void {
this.metrics.stop(runDetails);
if (runDetails.failedExpectations.length) {
const error = this.runDetailsToResult(runDetails);
this.metrics.errors.push(error);
this.display.failed(error);
}
this.summary.display(this.metrics);
}

Expand All @@ -86,7 +91,10 @@ export class SpecReporter implements CustomReporter {
}

public suiteDone(result: CustomReporterResult): void {
this.display.suiteDone();
this.display.suiteDone(result);
if (result.failedExpectations.length) {
this.metrics.errors.push(result);
}
}

public specStarted(result: CustomReporterResult): void {
Expand All @@ -107,4 +115,23 @@ export class SpecReporter implements CustomReporter {
this.display.failed(result);
}
}

private runDetailsToResult(runDetails: RunDetails): CustomReporterResult {
return {
description: "Non-spec failure",
failedExpectations: runDetails.failedExpectations.map(expectation => {
return {
actual: "",
expected: "",
matcherName: "",
message: expectation.message,
passed: false,
stack: (expectation as any).stack,
};
}),
fullName: "Non-spec failure",
id: "Non-spec failure",
};
}

}

0 comments on commit ddfd728

Please sign in to comment.