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

Add low-level print configuration option. #130

Merged
merged 1 commit into from
Apr 13, 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
1 change: 0 additions & 1 deletion spec/helpers/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Test {
this.outputs = [];
this.summary = [];
logInSummary = false;
// tslint:disable-next-line:no-unbound-method
console.log = stuff => {
if (!withColor) {
stuff = stuff.stripColors.stripTime();
Expand Down
122 changes: 122 additions & 0 deletions spec/unit/custom-print.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
describe("spec reporter", () => {

let outputs: string[];

describe("with custom print", () => {
beforeEach(() => {
outputs = [];
this.reporter = new global.SpecReporter({
colors: {
enabled: false,
},
print: line => {
outputs.push(line);
},
spec: {
displayPending: true
}
});
});

describe("when jasmine started", () => {
it("should report start with ", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("successful spec", () => {
this.passed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains(/Spec started/);
});
});

describe("when suite", () => {
it("should report suite with custom print", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("successful spec", () => {
this.passed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains(/suite/);
});
});

describe("when spec started", () => {
it("should report start", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("spec to be started", () => {
this.passed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains([
" suite",
" " + "✓ spec to be started".green
]);
});
});

describe("when spec done", () => {
it("should report success with custom print", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("successful spec", () => {
this.passed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains(/successful spec/);
});

it("should report failure with custom print", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("failed spec", () => {
this.failed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains([/failed spec/]);
});

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

it("should report pending with custom print", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.xit("pending spec", () => {
this.passed();
});
});
}).outputs.length).toEqual(0);
expect(outputs).contains(/pending spec/);
});
});

describe("when summary", () => {
it("should display summary error messages with custom print", () => {
expect(new Test(this.reporter, function() {
this.describe("suite", () => {
this.it("failed spec", () => {
this.failed();
});
});
}).summary.length).toEqual(0);
expect(outputs).contains([" - Expected true to be false."]);
});
});
});
});
1 change: 1 addition & 0 deletions src/configuration-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class ConfigurationParser {
pending: "* ",
successful: ConfigurationParser.isWindows ? "\u221A " : "✓ ",
},
print: stuff => console.log(stuff),
spec: {
displayDuration: false,
displayErrorMessages: true,
Expand Down
6 changes: 6 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ export class Configuration {
* options for custom processors
*/
public customOptions?: any;
/**
* Low-level printing function, defaults to console.log.
* Use process.stdout.write(log + '\n'); to avoid output to
* devtools console while still reporting to command line.
*/
public print?: (log: String) => void;
}
6 changes: 3 additions & 3 deletions src/display/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class Logger {
private currentIndent = "";
private lastWasNewLine = false;

constructor(private displayProcessors: DisplayProcessor[]) {
constructor(private displayProcessors: DisplayProcessor[], private print: (line: String) => void) {
}

public log(stuff: String): void {
stuff.split("\n").forEach((line: String) => {
console.log(line !== "" ? this.currentIndent + line : line);
this.print(line !== "" ? this.currentIndent + line : line);
});
this.lastWasNewLine = false;
}
Expand All @@ -30,7 +30,7 @@ export class Logger {

public newLine(): void {
if (!this.lastWasNewLine) {
console.log("");
this.log("");
this.lastWasNewLine = true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/spec-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export class SpecReporter implements CustomReporter {
constructor(configuration?: Configuration) {
this.configuration = ConfigurationParser.parse(configuration);
const displayProcessors = SpecReporter.initProcessors(this.configuration);
this.logger = new Logger(displayProcessors);
const print = this.configuration.print;
this.logger = new Logger(displayProcessors, print);
this.display = new ExecutionDisplay(this.configuration, this.logger, this.specs, displayProcessors);
this.summary = new SummaryDisplay(this.logger, this.configuration, this.specs);
this.metrics = new ExecutionMetrics();
Expand Down