Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reporting errors in afterAll. #214

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 34 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.globalErrors.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.globalErrors.length > 1) ? ` (${metrics.globalErrors.length} ERRORS)` : "";
errors = (metrics.globalErrors.length === 1) ? ` (${metrics.globalErrors.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.globalErrors.length > 0) {
this.errorsSummary(metrics.globalErrors);
}
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,30 @@ 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}`);
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 globalErrors: 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.globalErrors.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.globalErrors.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",
};
}

}