Skip to content

Commit

Permalink
Moves exit code reporting and defining an onComplete callback to an E…
Browse files Browse the repository at this point in the history
…xitCodeReporter

- Deprecates passing an onComplete to ConsoleReporter and as an option to configureDefaultReporter
- Adds an exit code reporter right before test execution
- Adds jasmine.onComplete that defines a callback for the exit code reporter to call
  • Loading branch information
Christopher Amavisca and Gregg Van Hove committed Feb 6, 2015
1 parent 92f47d3 commit 9cdc067
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 23 deletions.
6 changes: 5 additions & 1 deletion lib/console_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var noopTimer = {
function ConsoleReporter(options) {
var print = options.print,
showColors = options.showColors || false,
onComplete = options.onComplete || function() {},
timer = options.timer || noopTimer,
jasmineCorePath = options.jasmineCorePath,
specCount,
Expand All @@ -25,6 +24,11 @@ function ConsoleReporter(options) {
},
failedSuites = [];

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

this.jasmineStarted = function() {
specCount = 0;
failureCount = 0;
Expand Down
24 changes: 24 additions & 0 deletions lib/exit_code_reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = function() {
var results = true;
var onCompleteCallback = function() {};

this.onComplete = function(callback) {
onCompleteCallback = callback;
};

this.jasmineDone = function() {
onCompleteCallback(results);
};

this.specDone = function(result) {
if(result.status === 'failed') {
results = false;
}
};

this.suiteDone = function(result) {
if (result.failedExpectations && result.failedExpectations.length > 0) {
results = false;
}
};
};
38 changes: 27 additions & 11 deletions lib/jasmine.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var path = require('path'),
util = require('util'),
glob = require('glob'),
exit = require('exit');
exit = require('exit'),
ExitCodeReporter = require('./exit_code_reporter');

module.exports = Jasmine;
module.exports.ConsoleReporter = require('./console_reporter');
Expand All @@ -15,6 +16,9 @@ function Jasmine(options) {
this.specFiles = [];
this.env = this.jasmine.getEnv();
this.reportersCount = 0;
this.exitCodeReporter = new ExitCodeReporter();
this.onCompleteCallbackAdded = false;
this.exit = exit;
}

Jasmine.prototype.addSpecFile = function(filePath) {
Expand All @@ -27,23 +31,16 @@ Jasmine.prototype.addReporter = function(reporter) {
};

Jasmine.prototype.configureDefaultReporter = function(options) {
var defaultOnComplete = function(passed) {
if(passed) {
exit(0);
}
else {
exit(1);
}
};

options.timer = options.timer || new this.jasmine.Timer();
options.print = options.print || function() {
process.stdout.write(util.format.apply(this, arguments));
};
options.showColors = options.hasOwnProperty('showColors') ? options.showColors : true;
options.onComplete = options.onComplete || defaultOnComplete;
options.jasmineCorePath = options.jasmineCorePath || this.jasmineCorePath;

if(options.onComplete) {
console.warn('Passing in an onComplete function to configureDefaultReporter is deprecated.');
}
var consoleReporter = new module.exports.ConsoleReporter(options);
this.addReporter(consoleReporter);
};
Expand Down Expand Up @@ -97,6 +94,11 @@ Jasmine.prototype.addSpecFiles = function(files) {
});
};

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

Jasmine.prototype.execute = function(files) {
if(this.reportersCount === 0) {
this.configureDefaultReporter({});
Expand All @@ -109,5 +111,19 @@ Jasmine.prototype.execute = function(files) {
}

this.loadSpecs();

if(!this.onCompleteCallbackAdded) {
var jasmineRunner = this;
this.exitCodeReporter.onComplete(function(passed) {
if(passed) {
jasmineRunner.exit(0);
}
else {
jasmineRunner.exit(1);
}
});
}

this.addReporter(this.exitCodeReporter);
this.env.execute();
};
12 changes: 12 additions & 0 deletions spec/console_reporter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ describe("ConsoleReporter", function() {
}());
});

describe('when an onComplete function is passed in', function() {
it('warns the user of deprecation', function() {
spyOn(console, 'warn');
new ConsoleReporter({
onComplete: function () {}
});

expect(console.warn).toHaveBeenCalledWith('Passing in an onComplete function to the ConsoleReporter is deprecated.');
});
});

it("reports that the suite has started to the console", function() {
var reporter = new ConsoleReporter({
print: out.print
Expand Down Expand Up @@ -244,6 +255,7 @@ describe("ConsoleReporter", function() {
var onComplete, reporter;

beforeEach(function() {
spyOn(console, 'warn');
onComplete = jasmine.createSpy('onComplete');
reporter = new ConsoleReporter({
print: out.print,
Expand Down
52 changes: 52 additions & 0 deletions spec/exit_code_reporter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe('ExitCodeReporter', function() {
var ExitCodeReporter = require('../lib/exit_code_reporter');
var reporter, onComplete;

beforeEach(function() {
reporter = new ExitCodeReporter();
onComplete = jasmine.createSpy('onComplete');
reporter.onComplete(onComplete);
});

it('should report success with no specs', function() {
reporter.jasmineDone();

expect(onComplete).toHaveBeenCalledWith(true);
});

it('should report success with all successful specs', function() {
reporter.specDone({status: 'passed'});
reporter.specDone({status: 'pending'});

reporter.jasmineDone();

expect(onComplete).toHaveBeenCalledWith(true);
});

it('should report failure with any failing specs', function() {
reporter.specDone({status: 'passed'});
reporter.specDone({status: 'pending'});
reporter.specDone({status: 'failed'});

reporter.jasmineDone();

expect(onComplete).toHaveBeenCalledWith(false);
});

it('should report success with all passing suites', function() {
reporter.suiteDone({failedExpectations: []});
reporter.suiteDone({});

reporter.jasmineDone();

expect(onComplete).toHaveBeenCalledWith(true);
});

it('should report failure with any failing suites', function() {
reporter.suiteDone({failedExpectations: [{"some": 'stuff'}]});

reporter.jasmineDone();

expect(onComplete).toHaveBeenCalledWith(false);
});
});
67 changes: 62 additions & 5 deletions spec/jasmine_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ describe('Jasmine', function() {
});

describe('#configureDefaultReporter', function() {
it('creates a reporter with the passed in options', function() {
beforeEach(function() {
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});
});

it('creates a reporter with the passed in options', function() {
var reporterOptions = {
print: 'printer',
onComplete: 'on complete method',
showColors: true,
jasmineCorePath: 'path',
timer: 'timer'
Expand All @@ -64,23 +65,33 @@ describe('Jasmine', function() {
});

it('creates a reporter with a default option if an option is not specified', function() {
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});

var reporterOptions = {};

testJasmine.configureDefaultReporter(reporterOptions);

var expectedReporterOptions = {
print: jasmine.any(Function),
showColors: true,
onComplete: jasmine.any(Function),
timer: jasmine.any(Object),
jasmineCorePath: 'fake/jasmine/path/jasmine.js'
};

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

describe('passing in an onComplete function', function() {
it('warns the user of deprecation', function() {
spyOn(console, 'warn');
var reporterOptions = {
onComplete: function() {}
};

testJasmine.configureDefaultReporter(reporterOptions);

expect(console.warn).toHaveBeenCalledWith('Passing in an onComplete function to configureDefaultReporter is deprecated.');
});
});
});

it('adds matchers to the jasmine env', function() {
Expand Down Expand Up @@ -148,6 +159,16 @@ describe('Jasmine', function() {
});
});

describe('#onComplete', function() {
it('stores an onComplete function', function() {
var fakeOnCompleteCallback = function() {};
spyOn(testJasmine.exitCodeReporter, 'onComplete');

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

describe('#execute', function() {
it('uses the default console reporter if no reporters were added', function() {
spyOn(testJasmine, 'configureDefaultReporter');
Expand Down Expand Up @@ -187,5 +208,41 @@ describe('Jasmine', function() {

expect(relativePaths).toEqual(['/fixtures/sample_project/spec/fixture_spec.js', '/fixtures/sample_project/spec/other_fixture_spec.js']);
});

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

testJasmine.execute();

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

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

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

testJasmine.execute();
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](true);
expect(exitSpy).toHaveBeenCalledWith(0);
});

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

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

testJasmine.execute();
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](false);
expect(exitSpy).toHaveBeenCalledWith(1);
});
});
});
});
7 changes: 1 addition & 6 deletions tasks/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ module.exports = function(grunt) {
var done = this.async();

jasmine.loadConfigFile('./spec/support/jasmine.json');
jasmine.configureDefaultReporter({
onComplete: function(passed) {
done(passed);
}
});

jasmine.onComplete(done);
jasmine.execute();
});
};

0 comments on commit 9cdc067

Please sign in to comment.