Skip to content

Commit

Permalink
Output the first assertion. fixes avajs#121
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Nov 16, 2015
1 parent 2d36b51 commit 2b59169
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
11 changes: 6 additions & 5 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,19 @@ Runner.prototype.serial = function (tests) {
};

Runner.prototype._addTestResult = function (test) {
if (test.assertError) {
this.stats.failCount++;
}

var props = {
duration: test.duration,
title: test.title,
error: test.assertError,
error: undefined,
type: test.type,
skip: test.skip
};

if (test.assertErrors.length) {
this.stats.failCount++;
props.error = test.assertErrors[0];
}

this.results.push(props);
this.emit('test', props);
};
Expand Down
27 changes: 14 additions & 13 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function Test(title, fn) {
this.assertCount = 0;
this.planCount = null;
this.duration = null;
this.assertErrors = [];
this.context = {};

// test type, can be: test, hook, eachHook
Expand Down Expand Up @@ -73,14 +74,14 @@ Object.keys(assert).forEach(function (el) {
self._assert();
})
.catch(function (err) {
self.assertError = err;
self.assertErrors.push(err);
self._assert();
});
}

this._assert();
} catch (err) {
this.assertError = err;
this.assertErrors.push(err);
this._assert();
}
};
Expand Down Expand Up @@ -135,29 +136,29 @@ Test.prototype.run = function () {
}
}.bind(this))
.catch(function (err) {
this.assertError = new assert.AssertionError({
this.assertErrors.push(new assert.AssertionError({
actual: err,
message: 'Promise rejected → ' + err,
operator: 'promise'
});
}));

this.exit();
}.bind(this));
}
} catch (err) {
this.assertError = err;
this.assertErrors.push(err);
this.exit();
}
}.bind(this));
};

Test.prototype.end = function (err) {
if (err) {
this.assertError = new assert.AssertionError({
this.assertErrors.push(new assert.AssertionError({
actual: err,
message: 'Callback called with an error → ' + err,
operator: 'callback'
});
}));

return this.exit();
}
Expand All @@ -177,23 +178,23 @@ Test.prototype.exit = function () {
// stop infinite timer
clearTimeout(this._timeout);

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

this.assertError.stack = this.planStack;
planError.stack = this.planStack;
this.assertErrors.push(planError);
}

if (!this.ended) {
this.ended = true;

setImmediate(function () {
if (this.assertError) {
return this.promise.reject(this.assertError);
if (this.assertErrors.length) {
return this.promise.reject(this.assertErrors[0]);
}

this.promise.resolve(this);
Expand Down
11 changes: 11 additions & 0 deletions test/fixture/multiple-assert-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from '../../';

test(t => {
const a = 'foo';
t.ok(a === 'bar');

const b = 'kung';
t.ok(b === 'foo');

t.end();
});
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,18 @@ test('power-assert support', function (t) {
});
});

test('displays first assertion error', function (t) {
t.plan(3);

execCli('fixture/multiple-assert-errors.js', function (err, stdout, stderr) {
t.ok(err);

t.true((/t\.ok\(a === 'bar'\)/m).test(stderr));
// do not show the second by default
t.false((/t\.ok\(b === 'foo'\)/m).test(stderr));
});
});

test('circular references on assertions do not break process.send', function (t) {
t.plan(2);

Expand Down

0 comments on commit 2b59169

Please sign in to comment.