Skip to content

Commit 86ff25e

Browse files
committed
consistent error handling in test callbacks
Like when an error is thrown, or a rejected promise is returned, if the error is an Error instance use it as-is. Otherwise create an AssertionError.
1 parent bc326bd commit 86ff25e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ Object.defineProperty(Test.prototype, 'end', {
197197

198198
Test.prototype._end = function (err) {
199199
if (err) {
200-
this._setAssertError(new assert.AssertionError({
201-
actual: err,
202-
message: 'Callback called with an error → ' + err,
203-
operator: 'callback'
204-
}));
200+
if (!(err instanceof Error)) {
201+
err = new assert.AssertionError({
202+
actual: err,
203+
message: 'Callback called with an error: ' + inspect(err, {depth: null}),
204+
operator: 'callback'
205+
});
206+
}
207+
this._setAssertError(err);
205208

206209
this.exit();
207210
return;

test/test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,26 @@ test('end can be used as callback without maintaining thisArg', function (t) {
120120
});
121121

122122
test('end can be used as callback with error', function (t) {
123+
var err = new Error('failed');
123124
ava.cb(function (a) {
124-
a.end(new Error('failed'));
125+
a.end(err);
125126
}).run().then(function (result) {
126127
t.is(result.passed, false);
127-
t.true(result.reason instanceof Error);
128-
// TODO: Question - why not just set the reason to the error?
129-
t.match(result.reason.message, /Callback called with an error/);
128+
t.is(result.reason, err);
129+
t.end();
130+
});
131+
});
132+
133+
test('end can be used as callback with a non-error as its error argument', function (t) {
134+
var nonError = {foo: 'bar'};
135+
ava.cb(function (a) {
136+
a.end(nonError);
137+
}).run().then(function (result) {
138+
t.is(result.passed, false);
139+
t.ok(result.reason);
140+
t.is(result.reason.name, 'AssertionError');
141+
t.is(result.reason.actual, nonError);
142+
t.is(result.reason.message, 'Callback called with an error: { foo: \'bar\' }');
130143
t.end();
131144
});
132145
});

0 commit comments

Comments
 (0)