Skip to content

Commit

Permalink
Add low-level print configuration option.
Browse files Browse the repository at this point in the history
Defaults to console.log. Set to function process.stdout.write(log + '\n'); to avoid output to
devtools console while still reporting jasmine results to command line.
  • Loading branch information
johnjbarton committed Apr 7, 2017
1 parent bf1a2bf commit 8f465eb
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 8 deletions.
6 changes: 4 additions & 2 deletions spec/helpers/before-all.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
declare namespace NodeJS {
export interface Global {
TestHelper;
SpecReporter;
DisplayProcessor;
TestProcessor;
}
}

// require test-helper early to bind console.log
global.TestHelper = require("./test-helper");
global.SpecReporter = require("../../built/main").SpecReporter;
global.DisplayProcessor = require("../../built/main").DisplayProcessor;
global.TestProcessor = require("./test-processor").TestProcessor;

beforeAll(() => {
const addMatchers = require("./test-helper").addMatchers;
addMatchers();
global.TestHelper.addMatchers();
});
9 changes: 7 additions & 2 deletions spec/helpers/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ let addMatchers = () => {
});
};

// bind the global log function to our mockable symbol when this module is loaded.
let mockLog: (log: string) => void;
console.log = function attachMock() {
mockLog.apply(console, arguments);
};

class Test {
public outputs;
public summary;
Expand All @@ -72,8 +78,7 @@ class Test {
this.outputs = [];
this.summary = [];
logInSummary = false;
// tslint:disable-next-line:no-unbound-method
console.log = stuff => {
mockLog = stuff => {
if (!withColor) {
stuff = stuff.stripColors.stripTime();
}
Expand Down
121 changes: 121 additions & 0 deletions spec/unit/custom-print.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
describe("spec reporter", () => {

let outputs: Array<string>;

describe("with custom print", () => {
beforeEach(() => {
outputs = [];
this.reporter = new global.SpecReporter({
print: (line) => {
outputs.push(line);
},
colors: false,
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",
" ✓ spec to be started"
]);
});
});

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."]);
});
});
});
});
2 changes: 2 additions & 0 deletions src/configuration-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class ConfigurationParser {
pending: "* ",
successful: ConfigurationParser.isWindows ? "\u221A " : "✓ ",
},
// tslint:disable-next-line:no-unbound-method
print: console.log,
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

0 comments on commit 8f465eb

Please sign in to comment.