Skip to content

Commit

Permalink
fix handling of plan() not matching assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 20, 2015
1 parent 3ed5f9d commit ae4c266
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var util = require('util');
var assert = require('assert');
var EventEmitter = require('events').EventEmitter;
var fnName = require('fn-name');
var claim = require('claim');
Expand Down Expand Up @@ -32,9 +33,7 @@ function Test(title, fn) {

// TODO: find a better way to count assertions
this.onAssert(function () {
this.assertCount++;

if (this.assertCount === this.planCount) {
if (++this.assertCount === this.planCount) {
setImmediate(this.exit.bind(this));
}
}.bind(this));
Expand Down Expand Up @@ -114,7 +113,12 @@ Test.prototype.end = function () {

Test.prototype.exit = function () {
if (this.planCount !== null && this.planCount !== this.assertCount) {
throw new Error('Assertion count does not match planned');
this.assertError = new assert.AssertionError({
actual: this.assertCount,
expected: this.planCount,
message: 'Assertion count does not match planned',
operator: 'plan'
});
}

if (!this.ended) {
Expand Down
9 changes: 6 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ test('plan assertions', function (t) {
});
});

// TODO: fix how we throw assertion error when plan count doesn't match
test.skip('run more assertions than planned', function (t) {
test('run more assertions than planned', function (t) {
ava(function (a) {
a.plan(2);
a.true(true);
Expand Down Expand Up @@ -229,17 +228,20 @@ test('async assertion with `.end()`', function (t) {
});
});

test.skip('more assertions than planned should emit an assertion error', function (t) {
test('more assertions than planned should emit an assertion error', function (t) {
ava(function (a) {
a.plan(1);
a.pass();
a.pass();
}).run(function (err) {
t.true(err, err);
t.is(err.name, 'AssertionError');
t.end();
});
});

// NOTE(sindresorhus): I don't think this is possible as we won't know when the last assertion will happen, it could be minutes.
// Might be able to check `process._getActiveHandles().length === 1 && process._getActiveRequests().length === 0` or something.
test.skip('more assertions than planned should emit an assertion error - async', function (t) {
ava(function (a) {
a.plan(1);
Expand All @@ -250,6 +252,7 @@ test.skip('more assertions than planned should emit an assertion error - async',
}, 100);
}).run(function (err) {
t.true(err, err);
t.is(err.name, 'AssertionError');
t.end();
});
});
Expand Down

0 comments on commit ae4c266

Please sign in to comment.