Skip to content

Commit

Permalink
feature-detect process.exit() and "exit" events, exit tests now passes
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 4, 2012
1 parent 4ae9c04 commit 6fc71ba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
29 changes: 26 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ exports = module.exports = createHarness();
exports.createHarness = createHarness;
exports.Test = Test;

var canEmitExit = false;
var canExit = false;
//*
var canEmitExit = typeof process !== 'undefined' && process
&& typeof process.on === 'function'
;
var canExit = typeof process !== 'undefined' && process
&& typeof process.exit === 'function'
;
//*/

function createHarness () {
var pending = [];
var running = false;
Expand All @@ -15,6 +26,15 @@ function createHarness () {

var test = function (name, conf, cb) {
var t = new Test(name, conf, cb);
if (!conf || typeof conf !== 'object') conf = {};

if (conf.exit !== false && canEmitExit) {
process.on('exit', function (code) {
t._exit();
out.close();
if (code === 0 && !t._ok) process.exit(1);
});
}

process.nextTick(function () {
if (!out.piped) out.pipe(createDefaultStream());
Expand All @@ -40,7 +60,7 @@ function createHarness () {

t.on('end', onend);

return t
return t;

function onend () {
running = false;
Expand All @@ -56,8 +76,11 @@ function createHarness () {
}

process.nextTick(function () {
if (pending.length) pending.shift()()
else out.close()
if (pending.length) return pending.shift()();
out.close();
if (conf.exit !== false && canExit && !t._ok) {
process.exit(1);
}
});
}
};
Expand Down
17 changes: 17 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function Test (name_, opts_, cb_) {
this._plan = undefined;
this._cb = cb;
this._progeny = [];
this._ok = true;
}

Test.prototype.run = function () {
Expand Down Expand Up @@ -70,6 +71,21 @@ Test.prototype.end = function () {
this.ended = true;
};

Test.prototype._exit = function () {
if (this._plan !== undefined &&
!this._planError && this.assertCount !== this._plan) {
this._planError = true;
this.fail('plan != count', {
expected : this._plan,
actual : this.assertCount
});
}
else if (!this.ended) {
this.fail('test exited without ending');
}

};

Test.prototype._assert = function assert (ok, opts) {
var self = this;
var extra = opts.extra || {};
Expand All @@ -83,6 +99,7 @@ Test.prototype._assert = function assert (ok, opts) {
actual : defined(extra.actual, opts.actual),
expected : defined(extra.expected, opts.expected)
};
this._ok = Boolean(this._ok && ok);

if (!ok) {
res.error = defined(extra.error, opts.error, new Error(res.name));
Expand Down
2 changes: 1 addition & 1 deletion test/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ tap.test('too few exit', function (t) {
{ id: 3, ok: true, name: 'should be equivalent' },
{ id: 4, ok: true, name: 'should be equivalent' },
{ id: 5, ok: true, name: 'should be equivalent' },
{ id: 6, ok: false, name: 'too few assertions' },
{ id: 6, ok: false, name: 'plan != count' },
'tests 6',
'pass 5',
'fail 1'
Expand Down

0 comments on commit 6fc71ba

Please sign in to comment.