Skip to content

Commit a87cdad

Browse files
author
Gregg Van Hove
committed
Register ConsoleReporter immediately upon creation so it can be easily cleared
- No longer set it up as a default reporter - Rename ExitCodeReporter to CompletionReporter to be a bit less specific Fixes #88
1 parent 92adff2 commit a87cdad

File tree

6 files changed

+146
-164
lines changed

6 files changed

+146
-164
lines changed

lib/jasmine.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path'),
22
util = require('util'),
33
glob = require('glob'),
44
exit = require('./exit'),
5-
ExitCodeReporter = require('./reporters/exit_code_reporter'),
5+
CompletionReporter = require('./reporters/completion_reporter'),
66
ConsoleSpecFilter = require('./filters/console_spec_filter');
77

88
module.exports = Jasmine;
@@ -19,10 +19,17 @@ function Jasmine(options) {
1919
this.helperFiles = [];
2020
this.env = this.jasmine.getEnv();
2121
this.reportersCount = 0;
22-
this.exitCodeReporter = new ExitCodeReporter();
22+
this.completionReporter = new CompletionReporter();
2323
this.onCompleteCallbackAdded = false;
2424
this.exit = exit;
2525
this.showingColors = true;
26+
this.reporter = new module.exports.ConsoleReporter();
27+
this.env.addReporter(this.reporter);
28+
29+
var jasmineRunner = this;
30+
this.completionReporter.onComplete(function(passed) {
31+
jasmineRunner.exitCodeCompletion(passed);
32+
});
2633

2734
this.coreVersion = function() {
2835
return jasmineCore.version();
@@ -65,9 +72,7 @@ Jasmine.prototype.configureDefaultReporter = function(options) {
6572
if(options.onComplete) {
6673
this.printDeprecation('Passing in an onComplete function to configureDefaultReporter is deprecated.');
6774
}
68-
var consoleReporter = new module.exports.ConsoleReporter(options);
69-
this.provideFallbackReporter(consoleReporter);
70-
this.defaultReporterAdded = this.reportersCount === 0;
75+
this.reporter.setOptions(options);
7176
};
7277

7378
Jasmine.prototype.addMatchers = function(matchers) {
@@ -133,14 +138,22 @@ Jasmine.prototype.addSpecFiles = function(files) {
133138
};
134139

135140
Jasmine.prototype.onComplete = function(onCompleteCallback) {
136-
this.exitCodeReporter.onComplete(onCompleteCallback);
137-
this.onCompleteCallbackAdded = true;
141+
this.completionReporter.onComplete(onCompleteCallback);
138142
};
139143

140144
Jasmine.prototype.stopSpecOnExpectationFailure = function(value) {
141145
this.env.throwOnExpectationFailure(value);
142146
};
143147

148+
Jasmine.prototype.exitCodeCompletion = function(passed) {
149+
if(passed) {
150+
this.exit(0, process.platform, process.version, process.exit, require('exit'));
151+
}
152+
else {
153+
this.exit(1, process.platform, process.version, process.exit, require('exit'));
154+
}
155+
};
156+
144157
Jasmine.prototype.execute = function(files, filterString) {
145158
this.loadHelpers();
146159
this.configureDefaultReporter({ showColors: this.showingColors });
@@ -162,18 +175,6 @@ Jasmine.prototype.execute = function(files, filterString) {
162175

163176
this.loadSpecs();
164177

165-
if(!this.onCompleteCallbackAdded && this.defaultReporterAdded) {
166-
var jasmineRunner = this;
167-
this.exitCodeReporter.onComplete(function(passed) {
168-
if(passed) {
169-
jasmineRunner.exit(0, process.platform, process.version, process.exit, require('exit'));
170-
}
171-
else {
172-
jasmineRunner.exit(1, process.platform, process.version, process.exit, require('exit'));
173-
}
174-
});
175-
}
176-
177-
this.addReporter(this.exitCodeReporter);
178+
this.addReporter(this.completionReporter);
178179
this.env.execute();
179180
};

lib/reporters/console_reporter.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ var noopTimer = {
55
elapsed: function(){ return 0; }
66
};
77

8-
function ConsoleReporter(options) {
9-
var print = options.print,
10-
showColors = options.showColors || false,
11-
timer = options.timer || noopTimer,
12-
jasmineCorePath = options.jasmineCorePath,
13-
printDeprecation = options.printDeprecation || require('../printDeprecation'),
8+
function ConsoleReporter() {
9+
var print = function() {},
10+
showColors = false,
11+
timer = noopTimer,
12+
jasmineCorePath = null,
13+
printDeprecation = function() {},
1414
specCount,
1515
executableSpecCount,
1616
failureCount,
@@ -23,12 +23,22 @@ function ConsoleReporter(options) {
2323
none: '\x1B[0m'
2424
},
2525
failedSuites = [],
26+
stackFilter = defaultStackFilter,
27+
onComplete = function() {};
28+
29+
this.setOptions = function(options) {
30+
print = options.print;
31+
showColors = options.showColors || false;
32+
timer = options.timer || noopTimer;
33+
jasmineCorePath = options.jasmineCorePath;
34+
printDeprecation = options.printDeprecation || require('../printDeprecation');
2635
stackFilter = options.stackFilter || defaultStackFilter;
2736

28-
if(options.onComplete) {
29-
printDeprecation('Passing in an onComplete function to the ConsoleReporter is deprecated.');
30-
}
31-
var onComplete = options.onComplete || function() {};
37+
if(options.onComplete) {
38+
printDeprecation('Passing in an onComplete function to the ConsoleReporter is deprecated.');
39+
}
40+
onComplete = options.onComplete || function() {};
41+
};
3242

3343
this.jasmineStarted = function() {
3444
specCount = 0;

spec/jasmine_spec.js

Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,20 @@ describe('Jasmine', function() {
4545
expect(this.testJasmine.coreVersion()).toEqual('a version');
4646
});
4747

48+
it('registers a console reporter upon construction', function() {
49+
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});
50+
51+
var testJasmine = new Jasmine({ jasmineCore: this.fakeJasmineCore });
52+
53+
expect(testJasmine.env.addReporter).toHaveBeenCalledWith({someProperty: 'some value'});
54+
});
55+
4856
describe('#configureDefaultReporter', function() {
4957
beforeEach(function() {
50-
spyOn(Jasmine, 'ConsoleReporter').and.returnValue({someProperty: 'some value'});
58+
spyOn(this.testJasmine.reporter, 'setOptions');
5159
});
5260

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

6674
this.testJasmine.configureDefaultReporter(reporterOptions);
6775

68-
expect(Jasmine.ConsoleReporter).toHaveBeenCalledWith(expectedReporterOptions);
69-
expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalledWith({someProperty: 'some value'});
76+
expect(this.testJasmine.reporter.setOptions).toHaveBeenCalledWith(expectedReporterOptions);
7077
});
7178

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

84-
expect(Jasmine.ConsoleReporter).toHaveBeenCalledWith(expectedReporterOptions);
85-
expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalledWith({someProperty: 'some value'});
86-
});
87-
88-
describe('sets the defaultReportedAdded flag', function() {
89-
it('to true if the default reporter is used', function() {
90-
var reporterOptions = {};
91-
92-
this.testJasmine.configureDefaultReporter(reporterOptions);
93-
94-
expect(this.testJasmine.defaultReporterAdded).toBe(true);
95-
});
96-
97-
it('to false if the default reporter is not used', function() {
98-
var reporterOptions = {};
99-
var dummyReporter = {};
100-
101-
this.testJasmine.addReporter(dummyReporter);
102-
this.testJasmine.configureDefaultReporter(reporterOptions);
103-
104-
expect(this.testJasmine.defaultReporterAdded).toBe(false);
105-
});
91+
expect(this.testJasmine.reporter.setOptions).toHaveBeenCalledWith(expectedReporterOptions);
10692
});
10793

10894
describe('passing in an onComplete function', function() {
@@ -263,10 +249,10 @@ describe('Jasmine', function() {
263249
describe('#onComplete', function() {
264250
it('stores an onComplete function', function() {
265251
var fakeOnCompleteCallback = function() {};
266-
spyOn(this.testJasmine.exitCodeReporter, 'onComplete');
252+
spyOn(this.testJasmine.completionReporter, 'onComplete');
267253

268254
this.testJasmine.onComplete(fakeOnCompleteCallback);
269-
expect(this.testJasmine.exitCodeReporter.onComplete).toHaveBeenCalledWith(fakeOnCompleteCallback);
255+
expect(this.testJasmine.completionReporter.onComplete).toHaveBeenCalledWith(fakeOnCompleteCallback);
270256
});
271257
});
272258

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

305-
it('adds a default reporter as a fallback reporter', function() {
306-
this.testJasmine.addReporter(new Jasmine.ConsoleReporter({}));
307-
308-
//spyOn(this.testJasmine, 'configureDefaultReporter');
309-
spyOn(this.testJasmine, 'loadSpecs');
310-
311-
this.testJasmine.execute();
312-
313-
expect(this.testJasmine.env.provideFallbackReporter).toHaveBeenCalled();
314-
expect(this.testJasmine.env.addReporter).toHaveBeenCalled();
315-
expect(this.testJasmine.loadSpecs).toHaveBeenCalled();
316-
expect(this.testJasmine.env.execute).toHaveBeenCalled();
317-
});
318-
319291
it('loads helper files before checking if any reporters were added', function() {
320292
var loadHelpers = spyOn(this.testJasmine, 'loadHelpers');
321293
spyOn(this.testJasmine, 'configureDefaultReporter').and.callFake(function() {
@@ -351,56 +323,30 @@ describe('Jasmine', function() {
351323
});
352324

353325
it('adds an exit code reporter', function() {
354-
var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
355-
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;
326+
var completionReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
327+
this.testJasmine.completionReporter = completionReporterSpy;
356328
spyOn(this.testJasmine, 'addReporter');
357329

358330
this.testJasmine.execute();
359331

360-
expect(this.testJasmine.addReporter).toHaveBeenCalledWith(exitCodeReporterSpy);
332+
expect(this.testJasmine.addReporter).toHaveBeenCalledWith(completionReporterSpy);
361333
});
362334

363335
describe('default completion behavior', function() {
364-
describe('when the defaultReporterAdded flag is truthy', function() {
365-
beforeEach(function() {
366-
this.testJasmine.configureDefaultReporter({});
367-
});
368-
it('exits successfully when the whole suite is green', function() {
369-
var exitSpy = jasmine.createSpy('exit');
370-
this.testJasmine.exit = exitSpy;
336+
it('exits successfully when the whole suite is green', function() {
337+
var exitSpy = jasmine.createSpy('exit');
338+
this.testJasmine.exit = exitSpy;
371339

372-
var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
373-
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;
374-
375-
this.testJasmine.execute();
376-
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](true);
377-
expect(exitSpy).toHaveBeenCalledWith(0, process.platform, process.version, process.exit, require('exit'));
378-
});
379-
380-
it('exits with a failure when anything in the suite is not green', function() {
381-
var exitSpy = jasmine.createSpy('exit');
382-
this.testJasmine.exit = exitSpy;
383-
384-
var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
385-
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;
386-
387-
this.testJasmine.execute();
388-
exitCodeReporterSpy.onComplete.calls.mostRecent().args[0](false);
389-
expect(exitSpy).toHaveBeenCalledWith(1, process.platform, process.version, process.exit, require('exit'));
390-
});
340+
this.testJasmine.exitCodeCompletion(true);
341+
expect(exitSpy).toHaveBeenCalledWith(0, process.platform, process.version, process.exit, require('exit'));
391342
});
392343

393-
describe('when the defaultReporterAdded flag is falsy', function() {
394-
it('does not exit the process', function() {
395-
var exitSpy = jasmine.createSpy('exit');
396-
this.testJasmine.exit = exitSpy;
344+
it('exits with a failure when anything in the suite is not green', function() {
345+
var exitSpy = jasmine.createSpy('exit');
346+
this.testJasmine.exit = exitSpy;
397347

398-
var exitCodeReporterSpy = jasmine.createSpyObj('reporter', ['onComplete']);
399-
this.testJasmine.exitCodeReporter = exitCodeReporterSpy;
400-
401-
this.testJasmine.execute();
402-
expect(exitSpy).not.toHaveBeenCalled();
403-
});
348+
this.testJasmine.exitCodeCompletion(false);
349+
expect(exitSpy).toHaveBeenCalledWith(1, process.platform, process.version, process.exit, require('exit'));
404350
});
405351
});
406352
});

spec/reporters/exit_code_reporter_spec.js renamed to spec/reporters/completion_reporter_spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
describe('ExitCodeReporter', function() {
2-
var ExitCodeReporter = require('../../lib/reporters/exit_code_reporter');
1+
describe('CompletionReporter', function() {
2+
var CompletionReporter = require('../../lib/reporters/completion_reporter');
33

44
beforeEach(function() {
5-
this.reporter = new ExitCodeReporter();
5+
this.reporter = new CompletionReporter();
66
this.onComplete = jasmine.createSpy('onComplete');
77
this.reporter.onComplete(this.onComplete);
88
});

0 commit comments

Comments
 (0)