Skip to content

Commit 5710e65

Browse files
committed
ensure test errors are errors
If a test throws synchronously, but the error is not an Error instance, create an AssertionError like for rejected promises. Use inspect() to include a string representation of the actual error, and include it in the `actual` property. Remove special handling which converted `undefined` error values to the `'undefined'` string.
1 parent 2bd6ab8 commit 5710e65

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var co = require('co-with-promise');
77
var observableToPromise = require('observable-to-promise');
88
var isPromise = require('is-promise');
99
var isObservable = require('is-observable');
10+
var inspect = require('util').inspect;
1011
var assert = require('./assert');
1112
var enhanceAssert = require('./enhance-assert');
1213
var globals = require('./globals');
@@ -70,10 +71,6 @@ Test.prototype._setAssertError = function (err) {
7071
return;
7172
}
7273

73-
if (err === undefined) {
74-
err = 'undefined';
75-
}
76-
7774
this.assertError = err;
7875
};
7976

@@ -95,7 +92,15 @@ Test.prototype._run = function () {
9592
try {
9693
ret = this.fn(this._publicApi());
9794
} catch (err) {
98-
this._setAssertError(err);
95+
if (err instanceof Error) {
96+
this._setAssertError(err);
97+
} else {
98+
this._setAssertError(new assert.AssertionError({
99+
actual: err,
100+
message: 'Non-error thrown with value: ' + inspect(err, {depth: null}),
101+
operator: 'catch'
102+
}));
103+
}
99104
}
100105

101106
return ret;

test/test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,17 +356,24 @@ test('fails with thrown falsy value', function (t) {
356356
}).run();
357357

358358
t.is(result.passed, false);
359-
t.is(result.reason, 0);
359+
t.is(result.reason.actual, 0);
360+
t.is(result.reason.message, 'Non-error thrown with value: 0');
361+
t.is(result.reason.name, 'AssertionError');
362+
t.is(result.reason.operator, 'catch');
360363
t.end();
361364
});
362365

363-
test('throwing undefined will be converted to string "undefined"', function (t) {
366+
test('fails with thrown non-error object', function (t) {
367+
var obj = {foo: 'bar'};
364368
var result = ava(function () {
365-
throw undefined; // eslint-disable-line no-throw-literal
369+
throw obj;
366370
}).run();
367371

368372
t.is(result.passed, false);
369-
t.is(result.reason, 'undefined');
373+
t.is(result.reason.actual, obj);
374+
t.is(result.reason.message, 'Non-error thrown with value: { foo: \'bar\' }');
375+
t.is(result.reason.name, 'AssertionError');
376+
t.is(result.reason.operator, 'catch');
370377
t.end();
371378
});
372379

0 commit comments

Comments
 (0)