Skip to content

Commit 978431c

Browse files
grncdrJames Halliday
authored and
James Halliday
committed
Count subtests against the plan
1 parent bc03743 commit 978431c

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

lib/test.js

+56-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ var path = require('path');
55
var inherits = require('util').inherits;
66
var EventEmitter = require('events').EventEmitter;
77

8+
/**
9+
* Transitions:
10+
*
11+
* From -> To : Triggered by
12+
*
13+
* PENDING -> RUNNING : Results object calls .run
14+
* RUNNING -> FINISHED : .end no remaining subtest
15+
* RUNNING -> SUBTESTS : .end with subtests
16+
* SUBTESTS -> FINISHED : .end after all subtests end
17+
*
18+
* .end is called when:
19+
*
20+
* - it's called by the test itself
21+
* - there is a plan and:
22+
* - The assertion count + number of children == plan
23+
*
24+
*/
25+
26+
827
module.exports = Test;
928

1029
var nextTick = typeof setImmediate !== 'undefined'
@@ -36,6 +55,7 @@ function Test (name_, opts_, cb_) {
3655
this.readable = true;
3756
this.name = name || '(anonymous)';
3857
this.assertCount = 0;
58+
this.pendingCount = 0;
3959
this._skip = opts.skip || false;
4060
this._plan = undefined;
4161
this._cb = cb;
@@ -60,9 +80,19 @@ Test.prototype.run = function () {
6080
};
6181

6282
Test.prototype.test = function (name, opts, cb) {
83+
var self = this;
6384
var t = new Test(name, opts, cb);
6485
this._progeny.push(t);
86+
this.pendingCount++;
6587
this.emit('test', t);
88+
t.on('prerun', function () {
89+
self.assertCount++;
90+
})
91+
if (!self._pendingAsserts()) {
92+
nextTick(function () {
93+
self.end();
94+
});
95+
}
6696
};
6797

6898
Test.prototype.comment = function (msg) {
@@ -76,16 +106,19 @@ Test.prototype.plan = function (n) {
76106

77107
Test.prototype.end = function () {
78108
var self = this;
109+
79110
if (this._progeny.length) {
80111
var t = this._progeny.shift();
81-
t.on('end', function () { self.end() });
112+
t.on('end', function () {
113+
self.end();
114+
});
82115
this.emit('next', t);
83116
return;
84117
}
85118

86119
if (!this.ended) this.emit('end');
87-
if (this._plan !== undefined &&
88-
!this._planError && this.assertCount !== this._plan) {
120+
var pendingAsserts = this._pendingAsserts();
121+
if (!this._planError && this._plan !== undefined && pendingAsserts) {
89122
this._planError = true;
90123
this.fail('plan != count', {
91124
expected : this._plan,
@@ -112,6 +145,15 @@ Test.prototype._exit = function () {
112145
}
113146
};
114147

148+
Test.prototype._pendingAsserts = function () {
149+
if (this._plan === undefined) {
150+
return 1;
151+
} else {
152+
return this._plan -
153+
(this._progeny.length + this.assertCount);
154+
}
155+
}
156+
115157
Test.prototype._assert = function assert (ok, opts) {
116158
var self = this;
117159
var extra = opts.extra || {};
@@ -160,20 +202,22 @@ Test.prototype._assert = function assert (ok, opts) {
160202

161203
self.emit('result', res);
162204

163-
if (self._plan === self.assertCount && extra.exiting) {
164-
if (!self.ended) self.end();
165-
}
166-
else if (self._plan === self.assertCount) {
167-
nextTick(function () {
168-
if (!self.ended) self.end();
169-
});
205+
var pendingAsserts = self._pendingAsserts();
206+
if (!pendingAsserts) {
207+
if (extra.exiting) {
208+
self.end();
209+
} else {
210+
nextTick(function () {
211+
self.end();
212+
});
213+
}
170214
}
171215

172-
if (!self._planError && self.assertCount > self._plan) {
216+
if (!self._planError && pendingAsserts < 0) {
173217
self._planError = true;
174218
self.fail('plan != count', {
175219
expected : self._plan,
176-
actual : self.assertCount
220+
actual : self._plan - pendingAsserts
177221
});
178222
}
179223
};

test/nested.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tap.test('array test', function (tt) {
3939
test.createStream().pipe(tc);
4040

4141
test('nested array test', function (t) {
42-
t.plan(5);
42+
t.plan(6);
4343

4444
var src = '(' + function () {
4545
var xs = [ 1, 2, [ 3, 4 ] ];

test/subtest_plan.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('parent', function (t) {
1515

1616
t.test('second child', function (t) {
1717
t.plan(2);
18-
t.ok(firstChildRan, 'first child ran');
18+
t.ok(firstChildRan, 'first child ran first');
1919
t.pass('pass second child');
2020
});
2121
});

0 commit comments

Comments
 (0)