Skip to content

Commit

Permalink
Fix t.plan when the number of assertions is incorrect when the test e…
Browse files Browse the repository at this point in the history
…xits, but is correct once all assertions have resolved
  • Loading branch information
ariporad committed Dec 23, 2015
1 parent 7ffc043 commit 26f6fa9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
28 changes: 18 additions & 10 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ Test.prototype._end = function (err) {
Test.prototype.exit = function () {
var self = this;

function checkAssertLength() {
if (self.assertError === undefined && self.planCount !== null && self.planCount !== self.assertPromises.length) {
self._setAssertError(new assert.AssertionError({
actual: self.assertPromises.length,
expected: self.planCount,
message: 'Assertion count does not match planned',
operator: 'plan'
}));

self.assertError.stack = self.planStack;
return false;
}
return true;
}

checkAssertLength();

Promise.all(this.assertPromises)
.catch(function (err) {
self._setAssertError(err);
Expand All @@ -183,16 +200,7 @@ Test.prototype.exit = function () {
// stop infinite timer
globals.clearTimeout(self._timeout);

if (self.assertError === undefined && self.planCount !== null && self.planCount !== self.assertPromises.length) {
self._setAssertError(new assert.AssertionError({
actual: self.assertPromises.length,
expected: self.planCount,
message: 'Assertion count does not match planned',
operator: 'plan'
}));

self.assertError.stack = self.planStack;
}
checkAssertLength();

if (!self.ended) {
self.ended = true;
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,12 @@ test('number of assertions matches t.plan when the test exits, but before all pr
setTimeout(function () {
a.throws(Promise.reject(new Error('foo')), 'foo');
}, 5);
}).run().catch(function(err) {
}).run().catch(function (err) {
t.is(err.operator, 'plan');
t.is(err.actual, 3);
t.is(err.expected, 2);
t.end();
})
});
});

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

0 comments on commit 26f6fa9

Please sign in to comment.