Skip to content

Commit

Permalink
Register ConsoleReporter immediately upon creation so it can be easil…
Browse files Browse the repository at this point in the history
…y cleared

- No longer set it up as a default reporter
- Rename ExitCodeReporter to CompletionReporter to be a bit less
  specific

Fixes #88
  • Loading branch information
Gregg Van Hove committed Sep 17, 2016
1 parent 92adff2 commit a87cdad
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 164 deletions.
41 changes: 21 additions & 20 deletions lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path'),
util = require('util'),
glob = require('glob'),
exit = require('./exit'),
ExitCodeReporter = require('./reporters/exit_code_reporter'),
CompletionReporter = require('./reporters/completion_reporter'),
ConsoleSpecFilter = require('./filters/console_spec_filter');

module.exports = Jasmine;
Expand All @@ -19,10 +19,17 @@ function Jasmine(options) {
this.helperFiles = [];
this.env = this.jasmine.getEnv();
this.reportersCount = 0;
this.exitCodeReporter = new ExitCodeReporter();
this.completionReporter = new CompletionReporter();
this.onCompleteCallbackAdded = false;
this.exit = exit;
this.showingColors = true;
this.reporter = new module.exports.ConsoleReporter();
this.env.addReporter(this.reporter);

var jasmineRunner = this;
this.completionReporter.onComplete(function(passed) {
jasmineRunner.exitCodeCompletion(passed);
});

this.coreVersion = function() {
return jasmineCore.version();
Expand Down Expand Up @@ -65,9 +72,7 @@ Jasmine.prototype.configureDefaultReporter = function(options) {
if(options.onComplete) {
this.printDeprecation('Passing in an onComplete function to configureDefaultReporter is deprecated.');
}
var consoleReporter = new module.exports.ConsoleReporter(options);
this.provideFallbackReporter(consoleReporter);
this.defaultReporterAdded = this.reportersCount === 0;
this.reporter.setOptions(options);
};

Jasmine.prototype.addMatchers = function(matchers) {
Expand Down Expand Up @@ -133,14 +138,22 @@ Jasmine.prototype.addSpecFiles = function(files) {
};

Jasmine.prototype.onComplete = function(onCompleteCallback) {
this.exitCodeReporter.onComplete(onCompleteCallback);
this.onCompleteCallbackAdded = true;
this.completionReporter.onComplete(onCompleteCallback);
};

Jasmine.prototype.stopSpecOnExpectationFailure = function(value) {
this.env.throwOnExpectationFailure(value);
};

Jasmine.prototype.exitCodeCompletion = function(passed) {
if(passed) {
this.exit(0, process.platform, process.version, process.exit, require('exit'));
}
else {
this.exit(1, process.platform, process.version, process.exit, require('exit'));
}
};

Jasmine.prototype.execute = function(files, filterString) {
this.loadHelpers();
this.configureDefaultReporter({ showColors: this.showingColors });
Expand All @@ -162,18 +175,6 @@ Jasmine.prototype.execute = function(files, filterString) {

this.loadSpecs();

if(!this.onCompleteCallbackAdded && this.defaultReporterAdded) {
var jasmineRunner = this;
this.exitCodeReporter.onComplete(function(passed) {
if(passed) {
jasmineRunner.exit(0, process.platform, process.version, process.exit, require('exit'));
}
else {
jasmineRunner.exit(1, process.platform, process.version, process.exit, require('exit'));
}
});
}

this.addReporter(this.exitCodeReporter);
this.addReporter(this.completionReporter);
this.env.execute();
};
File renamed without changes.
30 changes: 20 additions & 10 deletions lib/reporters/console_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ var noopTimer = {
elapsed: function(){ return 0; }
};

function ConsoleReporter(options) {
var print = options.print,
showColors = options.showColors || false,
timer = options.timer || noopTimer,
jasmineCorePath = options.jasmineCorePath,
printDeprecation = options.printDeprecation || require('../printDeprecation'),
function ConsoleReporter() {
var print = function() {},
showColors = false,
timer = noopTimer,
jasmineCorePath = null,
printDeprecation = function() {},
specCount,
executableSpecCount,
failureCount,
Expand All @@ -23,12 +23,22 @@ function ConsoleReporter(options) {
none: '\x1B[0m'
},
failedSuites = [],
stackFilter = defaultStackFilter,
onComplete = function() {};

this.setOptions = function(options) {
print = options.print;
showColors = options.showColors || false;
timer = options.timer || noopTimer;
jasmineCorePath = options.jasmineCorePath;
printDeprecation = options.printDeprecation || require('../printDeprecation');
stackFilter = options.stackFilter || defaultStackFilter;

if(options.onComplete) {
printDeprecation('Passing in an onComplete function to the ConsoleReporter is deprecated.');
}
var onComplete = options.onComplete || function() {};
if(options.onComplete) {
printDeprecation('Passing in an onComplete function to the ConsoleReporter is deprecated.');
}
onComplete = options.onComplete || function() {};
};

this.jasmineStarted = function() {
specCount = 0;
Expand Down
108 changes: 27 additions & 81 deletions spec/jasmine_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ describe('Jasmine', function() {
expect(this.testJasmine.coreVersion()).toEqual('a version');
});

it('registers a console reporter upon construction', function() {
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});

var testJasmine = new Jasmine({ jasmineCore: this.fakeJasmineCore });

expect(testJasmine.env.addReporter).toHaveBeenCalledWith({someProperty: 'some value'});
});

describe('#configureDefaultReporter', function() {
beforeEach(function() {
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});
spyOn(this.testJasmine.reporter, 'setOptions');
});

it('creates a reporter with the passed in options', function() {
it('sets the options on the console reporter', function() {
var reporterOptions = {
print: 'printer',
showColors: true,
Expand All @@ -65,8 +73,7 @@ describe('Jasmine', function() {

this.testJasmine.configureDefaultReporter(reporterOptions);

expect(Jasmine.ConsoleReporter).toHaveBeenCalledWith(expectedReporterOptions);
expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalledWith({someProperty: 'some value'});
expect(this.testJasmine.reporter.setOptions).toHaveBeenCalledWith(expectedReporterOptions);
});

it('creates a reporter with a default option if an option is not specified', function() {
Expand All @@ -81,28 +88,7 @@ describe('Jasmine', function() {
jasmineCorePath: 'fake/jasmine/path/jasmine.js'
};

expect(Jasmine.ConsoleReporter).toHaveBeenCalledWith(expectedReporterOptions);
expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalledWith({someProperty: 'some value'});
});

describe('sets the defaultReportedAdded flag', function() {
it('to true if the default reporter is used', function() {
var reporterOptions = {};

this.testJasmine.configureDefaultReporter(reporterOptions);

expect(this.testJasmine.defaultReporterAdded).toBe(true);
});

it('to false if the default reporter is not used', function() {
var reporterOptions = {};
var dummyReporter = {};

this.testJasmine.addReporter(dummyReporter);
this.testJasmine.configureDefaultReporter(reporterOptions);

expect(this.testJasmine.defaultReporterAdded).toBe(false);
});
expect(this.testJasmine.reporter.setOptions).toHaveBeenCalledWith(expectedReporterOptions);
});

describe('passing in an onComplete function', function() {
Expand Down Expand Up @@ -263,10 +249,10 @@ describe('Jasmine', function() {
describe('#onComplete', function() {
it('stores an onComplete function', function() {
var fakeOnCompleteCallback = function() {};
spyOn(this.testJasmine.exitCodeReporter, 'onComplete');
spyOn(this.testJasmine.completionReporter, 'onComplete');

this.testJasmine.onComplete(fakeOnCompleteCallback);
expect(this.testJasmine.exitCodeReporter.onComplete).toHaveBeenCalledWith(fakeOnCompleteCallback);
expect(this.testJasmine.completionReporter.onComplete).toHaveBeenCalledWith(fakeOnCompleteCallback);
});
});

Expand Down Expand Up @@ -302,20 +288,6 @@ describe('Jasmine', function() {
expect(this.testJasmine.env.execute).toHaveBeenCalled();
});

it('adds a default reporter as a fallback reporter', function() {
this.testJasmine.addReporter(new Jasmine.ConsoleReporter({}));

//spyOn(this.testJasmine, 'configureDefaultReporter');
spyOn(this.testJasmine, 'loadSpecs');

this.testJasmine.execute();

expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalled();
expect(this.testJasmine.env.addReporter).toHaveBeenCalled();
expect(this.testJasmine.loadSpecs).toHaveBeenCalled();
expect(this.testJasmine.env.execute).toHaveBeenCalled();
});

it('loads helper files before checking if any reporters were added', function() {
var loadHelpers = spyOn(this.testJasmine, 'loadHelpers');
spyOn(this.testJasmine, 'configureDefaultReporter').and.callFake(function() {
Expand Down Expand Up @@ -351,56 +323,30 @@ describe('Jasmine', function() {
});

it('adds an exit code reporter', function() {
var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;
var completionReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
this.testJasmine.completionReporter = completionReporterSpy;
spyOn(this.testJasmine, 'addReporter');

this.testJasmine.execute();

expect(this.testJasmine.addReporter).toHaveBeenCalledWith(exitCodeReporterSpy);
expect(this.testJasmine.addReporter).toHaveBeenCalledWith(completionReporterSpy);
});

describe('default completion behavior', function() {
describe('when the defaultReporterAdded flag is truthy', function() {
beforeEach(function() {
this.testJasmine.configureDefaultReporter({});
});
it('exits successfully when the whole suite is green', function() {
var exitSpy = jasmine.createSpy('exit');
this.testJasmine.exit = exitSpy;
it('exits successfully when the whole suite is green', function() {
var exitSpy = jasmine.createSpy('exit');
this.testJasmine.exit = exitSpy;

var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;

this.testJasmine.execute();
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](true);
expect(exitSpy).toHaveBeenCalledWith(0, process.platform, process.version, process.exit, require('exit'));
});

it('exits with a failure when anything in the suite is not green', function() {
var exitSpy = jasmine.createSpy('exit');
this.testJasmine.exit = exitSpy;

var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;

this.testJasmine.execute();
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](false);
expect(exitSpy).toHaveBeenCalledWith(1, process.platform, process.version, process.exit, require('exit'));
});
this.testJasmine.exitCodeCompletion(true);
expect(exitSpy).toHaveBeenCalledWith(0, process.platform, process.version, process.exit, require('exit'));
});

describe('when the defaultReporterAdded flag is falsy', function() {
it('does not exit the process', function() {
var exitSpy = jasmine.createSpy('exit');
this.testJasmine.exit = exitSpy;
it('exits with a failure when anything in the suite is not green', function() {
var exitSpy = jasmine.createSpy('exit');
this.testJasmine.exit = exitSpy;

var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;

this.testJasmine.execute();
expect(exitSpy).not.toHaveBeenCalled();
});
this.testJasmine.exitCodeCompletion(false);
expect(exitSpy).toHaveBeenCalledWith(1, process.platform, process.version, process.exit, require('exit'));
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
describe('ExitCodeReporter', function() {
var ExitCodeReporter = require('../../lib/reporters/exit_code_reporter');
describe('CompletionReporter', function() {
var CompletionReporter = require('../../lib/reporters/completion_reporter');

beforeEach(function() {
this.reporter = new ExitCodeReporter();
this.reporter = new CompletionReporter();
this.onComplete = jasmine.createSpy('onComplete');
this.reporter.onComplete(this.onComplete);
});
Expand Down
Loading

0 comments on commit a87cdad

Please sign in to comment.