Skip to content

Commit

Permalink
Close #259 PR: print the first assertion failure of each test, instea…
Browse files Browse the repository at this point in the history
…d of the last. Fixes #121, #220. Fixes #121, Fixes #220
  • Loading branch information
jamestalmage authored and sindresorhus committed Nov 23, 2015
1 parent 293acf2 commit eb9b339
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Test(title, fn) {
this.assertCount = 0;
this.planCount = null;
this.duration = null;
this.assertError = undefined;

// test type, can be: test, hook, eachHook
this.type = 'test';
Expand Down Expand Up @@ -67,20 +68,32 @@ 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) {
return;
}

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 @@ -118,7 +131,7 @@ Test.prototype.run = function () {
try {
ret = this.fn(this);
} catch (err) {
this.assertError = err;
this._setAssertError(err);
this.exit();
}

Expand All @@ -132,11 +145,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 @@ -147,11 +160,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 @@ -174,13 +187,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 @@ -189,7 +202,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
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ test(t => {
});
```

If multiple assertion failures are encountered within a single test, AVA will only display the *first* one.

### .pass([message])

Passing assertion.
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('fails 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('fails with thrown falsy 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 eb9b339

Please sign in to comment.