Skip to content

Commit

Permalink
Add displaySpecErrorMessages and displaySummaryErrorMessages to D…
Browse files Browse the repository at this point in the history
…isplayProcessor #85
  • Loading branch information
bcaudan committed Jan 4, 2017
1 parent 56e38e4 commit 6e1653e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.1.0

* Add `displaySpecErrorMessages` and `displaySummaryErrorMessages` to DisplayProcessor [#85](https://github.com/bcaudan/jasmine-spec-reporter/issues/85)

# 3.0.0

## Breaking changes
Expand Down
2 changes: 2 additions & 0 deletions docs/customize-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ You can then customize the following methods:
* displaySpecStarted(spec, log)
* displaySuccessfulSpec(spec, log)
* displayFailedSpec(spec, log)
* displaySpecErrorMessages(spec, log)
* displaySummaryErrorMessages(spec, log)
* displayPendingSpec(spec, log)

The first argument is the jasmine object corresponding to the suite or the spec. The second argument is the log to be displayed. Those methods should return the modified log.
Expand Down
24 changes: 23 additions & 1 deletion spec/custom-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ describe("spec reporter", () => {
this.failed();
});
});
}).outputs).contains(/failed spec TEST/);
}).outputs).contains([/failed spec TEST/]);
});

it("should display spec error messages with custom display", () => {
expect(new Test(this.reporter, function () {
this.describe("suite", () => {
this.it("failed spec", () => {
this.failed();
});
});
}).outputs).contains([" - Expected true to be false. TEST"]);
});

it("should report pending with custom display", () => {
Expand All @@ -79,5 +89,17 @@ describe("spec reporter", () => {
}).outputs).contains(/pending spec TEST/);
});
});

describe("when summary", () => {
it("should display summary error messages with custom display", () => {
expect(new Test(this.reporter, function () {
this.describe("suite", () => {
this.it("failed spec", () => {
this.failed();
});
});
}).summary).contains([" - Expected true to be false. TEST"]);
});
});
});
});
2 changes: 1 addition & 1 deletion spec/display-stacktrace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("with display stacktrace", () => {
});
}).outputs;
expect(outputs).not.contains(/passed assertion/);
expect(outputs).contains([ " ✗ failed spec", " - Expected true to be false.", /at Object\.<anonymous>/, "" ]);
expect(outputs).contains([ " ✗ failed spec", " - Expected true to be false.", /at Env\.env\.failed/, /at Object\.<anonymous>/, "" ]);
});
});

Expand Down
8 changes: 8 additions & 0 deletions spec/helpers/test-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export class TestProcessor extends DisplayProcessor {
return log + this.test;
}

displaySpecErrorMessages(spec: any, log: String): String {
return log + this.test;
}

displaySummaryErrorMessages(spec: any, log: String): String {
return log + this.test;
}

displayPendingSpec(spec: any, log: String): String {
return log + this.test;
}
Expand Down
8 changes: 8 additions & 0 deletions src/display/display-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export class DisplayProcessor {
return log;
}

displaySpecErrorMessages(spec: any, log: String): String {
return log;
}

displaySummaryErrorMessages(spec: any, log: String): String {
return log;
}

displayPendingSpec(spec: any, log: String): String {
return log;
}
Expand Down
38 changes: 38 additions & 0 deletions src/display/processors/default-processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { DisplayProcessor } from "../display-processor";
import { Configuration } from "../../configuration";

export class DefaultProcessor extends DisplayProcessor {
private configuration: Configuration;

constructor(configuration: Configuration) {
super(configuration);
this.configuration = configuration;
}

displayJasmineStarted(): String {
return "Spec started";
}
Expand All @@ -17,11 +25,41 @@ export class DefaultProcessor extends DisplayProcessor {
return DefaultProcessor.displaySpecDescription(spec);
}

displaySpecErrorMessages(spec: any): String {
return DefaultProcessor.displayErrorMessages(spec, this.configuration.spec.displayStacktrace);
}

displaySummaryErrorMessages(spec: any): String {
return DefaultProcessor.displayErrorMessages(spec, this.configuration.summary.displayStacktrace);
}

displayPendingSpec(spec: any): String {
return DefaultProcessor.displaySpecDescription(spec);
}

private static displaySpecDescription(spec: any): String {
return spec.description;
}

private static displayErrorMessages(spec: any, withStacktrace: boolean): String {
let logs: String[] = [];
for (let i: number = 0; i < spec.failedExpectations.length; i++) {
logs.push("- ".failed + spec.failedExpectations[i].message.failed);
if (withStacktrace && spec.failedExpectations[i].stack) {
logs.push(DefaultProcessor.filterStackTraces(spec.failedExpectations[i].stack));
}
}
return logs.join("\n");
}

private static filterStackTraces(traces: string): string {
let lines: string[] = traces.split("\n");
let filtered: string[] = [];
for (let i: number = 1; i < lines.length; i++) {
if (!/(jasmine[^\/]*\.js|Timer\.listOnTimeout)/.test(lines[i])) {
filtered.push(lines[i]);
}
}
return filtered.join("\n");
}
}
38 changes: 13 additions & 25 deletions src/execution-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export class ExecutionDisplay {

failedSummary(spec: any, index: number): void {
this.log(`${index}) ${spec.fullName}`);
this.displayErrorMessages(spec, this.configuration.summary.displayStacktrace);
this.increaseIndent();
this.process(spec, (displayProcessor: DisplayProcessor, spec: any, log: String): String => {
return displayProcessor.displaySummaryErrorMessages(spec, log);
});
this.decreaseIndent();
}

pendingsSummary(): void {
Expand Down Expand Up @@ -174,7 +178,11 @@ export class ExecutionDisplay {
this.process(spec, (displayProcessor: DisplayProcessor, spec: any, log: String): String => {
return displayProcessor.displayFailedSpec(spec, log);
});
this.displayErrorMessages(spec, this.configuration.spec.displayStacktrace);
this.increaseIndent();
this.process(spec, (displayProcessor: DisplayProcessor, spec: any, log: String): String => {
return displayProcessor.displaySpecErrorMessages(spec, log);
});
this.decreaseIndent();
}
}

Expand All @@ -188,28 +196,6 @@ export class ExecutionDisplay {
}
}

displayErrorMessages(spec: any, withStacktrace: boolean): void {
this.increaseIndent();
for (let i: number = 0; i < spec.failedExpectations.length; i++) {
this.log("- ".failed + spec.failedExpectations[i].message.failed);
if (withStacktrace && spec.failedExpectations[i].stack) {
this.log(this.filterStackTraces(spec.failedExpectations[i].stack));
}
}
this.decreaseIndent();
}

filterStackTraces(traces: string): string {
let lines: string[] = traces.split("\n");
let filtered: string[] = [];
for (let i: number = 1; i < lines.length; i++) {
if (!/(jasmine[^\/]*\.js|Timer\.listOnTimeout)/.test(lines[i])) {
filtered.push(lines[i]);
}
}
return filtered.join(`\n${this.currentIndent}`);
}

suiteStarted(suite: any): void {
this.suiteHierarchy.push(suite);
}
Expand Down Expand Up @@ -263,7 +249,9 @@ export class ExecutionDisplay {

log(stuff: String): void {
if (stuff !== null) {
console.log(this.currentIndent + stuff);
stuff.split("\n").forEach((line: String) => {
console.log(line !== "" ? this.currentIndent + line : line);
});
this.lastWasNewLine = false;
}
}
Expand Down

0 comments on commit 6e1653e

Please sign in to comment.