Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer early returns over nesting when possible:

if (this.assertError !== undefined) {
    return;
}

if (err === undefined) {
    err = 'undefined';
}

this.assertError = err;


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();
});
});