Skip to content

Commit 6fc71ba

Browse files
author
James Halliday
committedDec 4, 2012
feature-detect process.exit() and "exit" events, exit tests now passes
1 parent 4ae9c04 commit 6fc71ba

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed
 

‎index.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ exports = module.exports = createHarness();
66
exports.createHarness = createHarness;
77
exports.Test = Test;
88

9+
var canEmitExit = false;
10+
var canExit = false;
11+
//*
12+
var canEmitExit = typeof process !== 'undefined' && process
13+
&& typeof process.on === 'function'
14+
;
15+
var canExit = typeof process !== 'undefined' && process
16+
&& typeof process.exit === 'function'
17+
;
18+
//*/
19+
920
function createHarness () {
1021
var pending = [];
1122
var running = false;
@@ -15,6 +26,15 @@ function createHarness () {
1526

1627
var test = function (name, conf, cb) {
1728
var t = new Test(name, conf, cb);
29+
if (!conf || typeof conf !== 'object') conf = {};
30+
31+
if (conf.exit !== false && canEmitExit) {
32+
process.on('exit', function (code) {
33+
t._exit();
34+
out.close();
35+
if (code === 0 && !t._ok) process.exit(1);
36+
});
37+
}
1838

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

4161
t.on('end', onend);
4262

43-
return t
63+
return t;
4464

4565
function onend () {
4666
running = false;
@@ -56,8 +76,11 @@ function createHarness () {
5676
}
5777

5878
process.nextTick(function () {
59-
if (pending.length) pending.shift()()
60-
else out.close()
79+
if (pending.length) return pending.shift()();
80+
out.close();
81+
if (conf.exit !== false && canExit && !t._ok) {
82+
process.exit(1);
83+
}
6184
});
6285
}
6386
};

‎lib/test.js

+17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Test (name_, opts_, cb_) {
3232
this._plan = undefined;
3333
this._cb = cb;
3434
this._progeny = [];
35+
this._ok = true;
3536
}
3637

3738
Test.prototype.run = function () {
@@ -70,6 +71,21 @@ Test.prototype.end = function () {
7071
this.ended = true;
7172
};
7273

74+
Test.prototype._exit = function () {
75+
if (this._plan !== undefined &&
76+
!this._planError && this.assertCount !== this._plan) {
77+
this._planError = true;
78+
this.fail('plan != count', {
79+
expected : this._plan,
80+
actual : this.assertCount
81+
});
82+
}
83+
else if (!this.ended) {
84+
this.fail('test exited without ending');
85+
}
86+
87+
};
88+
7389
Test.prototype._assert = function assert (ok, opts) {
7490
var self = this;
7591
var extra = opts.extra || {};
@@ -83,6 +99,7 @@ Test.prototype._assert = function assert (ok, opts) {
8399
actual : defined(extra.actual, opts.actual),
84100
expected : defined(extra.expected, opts.expected)
85101
};
102+
this._ok = Boolean(this._ok && ok);
86103

87104
if (!ok) {
88105
res.error = defined(extra.error, opts.error, new Error(res.name));

‎test/exit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ tap.test('too few exit', function (t) {
9393
{ id: 3, ok: true, name: 'should be equivalent' },
9494
{ id: 4, ok: true, name: 'should be equivalent' },
9595
{ id: 5, ok: true, name: 'should be equivalent' },
96-
{ id: 6, ok: false, name: 'too few assertions' },
96+
{ id: 6, ok: false, name: 'plan != count' },
9797
'tests 6',
9898
'pass 5',
9999
'fail 1'

0 commit comments

Comments
 (0)
Please sign in to comment.