Skip to content

Commit

Permalink
print the first assertion failure of each test, instead of the last. F…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Nov 23, 2015
1 parent 60b2337 commit 88dab1f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
32 changes: 21 additions & 11 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.planCount = null;
this.duration = null;
this.context = {};
this.assertError = undefined;

// test type, can be: test, hook, eachHook
this.type = 'test';
Expand Down Expand Up @@ -68,20 +69,29 @@ Object.keys(assert).forEach(function (el) {
if (isPromise(fn)) {
return Promise.resolve(fn)
.catch(function (err) {
self.assertError = err;
self._setAssertError(err);
})
.finally(function () {
self._assert();
});
}
} catch (err) {
this.assertError = err;
this._setAssertError(err);
}

this._assert();
};
});

Test.prototype._setAssertError = function (err) {
if (this.assertError === undefined) {
if (err === undefined) {
err = 'undefined';
}
this.assertError = err;
}
};

// Workaround for power-assert
// `t` must be capturable for decorated assert output
Test.prototype._capt = assert._capt;
Expand Down Expand Up @@ -119,7 +129,7 @@ Test.prototype.run = function () {
try {
ret = this.fn(this);
} catch (err) {
this.assertError = err;
this._setAssertError(err);
this.exit();
}

Expand All @@ -133,11 +143,11 @@ Test.prototype.run = function () {
}
})
.catch(function (err) {
self.assertError = new assert.AssertionError({
self._setAssertError(new assert.AssertionError({
actual: err,
message: 'Promise rejected → ' + err,
operator: 'promise'
});
}));

self.exit();
});
Expand All @@ -148,11 +158,11 @@ Test.prototype.run = function () {

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

this.exit();
return;
Expand All @@ -175,13 +185,13 @@ 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.assertError === undefined && this.planCount !== null && this.planCount !== this.assertCount) {
this._setAssertError(new assert.AssertionError({
actual: this.assertCount,
expected: this.planCount,
message: 'Assertion count does not match planned',
operator: 'plan'
});
}));

this.assertError.stack = this.planStack;
}
Expand All @@ -190,7 +200,7 @@ Test.prototype.exit = function () {
this.ended = true;

setImmediate(function () {
if (self.assertError) {
if (self.assertError !== undefined) {
self.promise.reject(self.assertError);
return;
}
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,33 @@ test('wait for test to end', function (t) {
avaTest.pass();
}, 1234);
});

test('promise is rejected with the first assertError', function (t) {
ava(function (a) {
a.plan(2);
a.is(1, 2);
a.is(3, 4);
}).run().catch(function (err) {
t.is(err.actual, 1);
t.is(err.expected, 2);
t.end();
});
});

test('promise is rejected with thrown falsie value', function (t) {
ava(function () {
throw 0; // eslint-disable-line no-throw-literal
}).run().catch(function (err) {
t.equal(err, 0);
t.end();
});
});

test('throwing undefined will be converted to string "undefined"', function (t) {
ava(function () {
throw undefined; // eslint-disable-line no-throw-literal
}).run().catch(function (err) {
t.equal(err, 'undefined');
t.end();
});
});

0 comments on commit 88dab1f

Please sign in to comment.