Skip to content

Commit

Permalink
moved exit listener add and removal to completion reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
battk committed Jan 23, 2019
1 parent b8183d2 commit 8e9ab27
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ var checkExit = function(jasmineRunner) {
};

Jasmine.prototype.execute = function(files, filterString) {
process.on('exit', this.checkExit);
this.completionReporter.exitHandler = this.checkExit;

this.loadRequires();
this.loadHelpers();
Expand Down
12 changes: 12 additions & 0 deletions lib/reporters/completion_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ module.exports = function() {
onCompleteCallback = callback;
};

this.jasmineStarted = function() {
if (this.exitHandler) {
process.on('exit', this.exitHandler);
}
};

this.jasmineDone = function(result) {
completed = true;
if (this.exitHandler) {
process.off('exit', this.exitHandler);
}

onCompleteCallback(result.overallStatus === 'passed');
};

this.isComplete = function() {
return completed;
};

this.exitHandler = null;
};
7 changes: 1 addition & 6 deletions spec/jasmine_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ describe('Jasmine', function() {
this.testJasmine = new Jasmine({ jasmineCore: this.fakeJasmineCore });
});

afterEach(function() {
if (this.testJasmine.checkExit) {
process.removeListener('exit', this.testJasmine.checkExit);
}
});

describe('constructor options', function() {
it('have defaults', function() {
expect(this.testJasmine.projectBaseDir).toEqual(path.resolve());
Expand Down Expand Up @@ -410,6 +404,7 @@ describe('Jasmine', function() {
this.testJasmine.execute();

expect(this.testJasmine.addReporter).toHaveBeenCalledWith(completionReporterSpy);
expect(this.testJasmine.completionReporter.exitHandler).toBe(this.testJasmine.checkExit);
});

describe('when exit is called prematurely', function() {
Expand Down
19 changes: 19 additions & 0 deletions spec/reporters/completion_reporter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe('CompletionReporter', function() {
this.reporter = new CompletionReporter();
this.onComplete = jasmine.createSpy('onComplete');
this.reporter.onComplete(this.onComplete);
this.processOn = spyOn(process, 'on').and.callThrough();
this.processOff = spyOn(process, 'off').and.callThrough();
});

describe('When the overall status is "passed"', function() {
Expand All @@ -20,4 +22,21 @@ describe('CompletionReporter', function() {
expect(this.onComplete).toHaveBeenCalledWith(false);
});
});

describe('When jasmine is started and done', function() {
it('adds and removes the exit handler', function() {
this.reporter.exitHandler = function() {};
this.reporter.jasmineStarted();
this.reporter.jasmineDone({overallStatus: 'passed'});
expect(this.processOn).toHaveBeenCalledWith('exit', this.reporter.exitHandler);
expect(this.processOff).toHaveBeenCalledWith('exit', this.reporter.exitHandler);
});

it('ignores the exit event if there is no exit handler', function() {
this.reporter.jasmineStarted();
this.reporter.jasmineDone({overallStatus: 'passed'});
expect(this.processOn).toHaveBeenCalledTimes(0);
expect(this.processOff).toHaveBeenCalledTimes(0);
});
});
});

0 comments on commit 8e9ab27

Please sign in to comment.