Skip to content

Commit 26f6fa9

Browse files
committed
Fix t.plan when the number of assertions is incorrect when the test exits, but is correct once all assertions have resolved
1 parent 7ffc043 commit 26f6fa9

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/test.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ Test.prototype._end = function (err) {
172172
Test.prototype.exit = function () {
173173
var self = this;
174174

175+
function checkAssertLength() {
176+
if (self.assertError === undefined && self.planCount !== null && self.planCount !== self.assertPromises.length) {
177+
self._setAssertError(new assert.AssertionError({
178+
actual: self.assertPromises.length,
179+
expected: self.planCount,
180+
message: 'Assertion count does not match planned',
181+
operator: 'plan'
182+
}));
183+
184+
self.assertError.stack = self.planStack;
185+
return false;
186+
}
187+
return true;
188+
}
189+
190+
checkAssertLength();
191+
175192
Promise.all(this.assertPromises)
176193
.catch(function (err) {
177194
self._setAssertError(err);
@@ -183,16 +200,7 @@ Test.prototype.exit = function () {
183200
// stop infinite timer
184201
globals.clearTimeout(self._timeout);
185202

186-
if (self.assertError === undefined && self.planCount !== null && self.planCount !== self.assertPromises.length) {
187-
self._setAssertError(new assert.AssertionError({
188-
actual: self.assertPromises.length,
189-
expected: self.planCount,
190-
message: 'Assertion count does not match planned',
191-
operator: 'plan'
192-
}));
193-
194-
self.assertError.stack = self.planStack;
195-
}
203+
checkAssertLength();
196204

197205
if (!self.ended) {
198206
self.ended = true;

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,12 @@ test('number of assertions matches t.plan when the test exits, but before all pr
452452
setTimeout(function () {
453453
a.throws(Promise.reject(new Error('foo')), 'foo');
454454
}, 5);
455-
}).run().catch(function(err) {
455+
}).run().catch(function (err) {
456456
t.is(err.operator, 'plan');
457457
t.is(err.actual, 3);
458458
t.is(err.expected, 2);
459459
t.end();
460-
})
460+
});
461461
});
462462

463463
test('number of assertions doesn\'t t.plan when the test exits, but before all promises resolve another is added', function (t) {
@@ -468,7 +468,7 @@ test('number of assertions doesn\'t t.plan when the test exits, but before all p
468468
setTimeout(function () {
469469
a.throws(Promise.reject(new Error('foo')), 'foo');
470470
}, 5);
471-
}).run().catch(function(err) {
471+
}).run().catch(function (err) {
472472
t.is(err.operator, 'plan');
473473
t.is(err.actual, 2);
474474
t.is(err.expected, 3);

0 commit comments

Comments
 (0)