Browse Source

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.
babel-plugin-for-integration-tests
Mark Wubben 9 years ago
parent
commit
5710e65d93
  1. 15
      lib/test.js
  2. 15
      test/test.js

15
lib/test.js

@ -7,6 +7,7 @@ var co = require('co-with-promise');
var observableToPromise = require('observable-to-promise');
var isPromise = require('is-promise');
var isObservable = require('is-observable');
var inspect = require('util').inspect;
var assert = require('./assert');
var enhanceAssert = require('./enhance-assert');
var globals = require('./globals');
@ -70,10 +71,6 @@ Test.prototype._setAssertError = function (err) {
return;
}
if (err === undefined) {
err = 'undefined';
}
this.assertError = err;
};
@ -95,7 +92,15 @@ Test.prototype._run = function () {
try {
ret = this.fn(this._publicApi());
} catch (err) {
this._setAssertError(err);
if (err instanceof Error) {
this._setAssertError(err);
} else {
this._setAssertError(new assert.AssertionError({
actual: err,
message: 'Non-error thrown with value: ' + inspect(err, {depth: null}),
operator: 'catch'
}));
}
}
return ret;

15
test/test.js

@ -356,17 +356,24 @@ test('fails with thrown falsy value', function (t) {
}).run();
t.is(result.passed, false);
t.is(result.reason, 0);
t.is(result.reason.actual, 0);
t.is(result.reason.message, 'Non-error thrown with value: 0');
t.is(result.reason.name, 'AssertionError');
t.is(result.reason.operator, 'catch');
t.end();
});
test('throwing undefined will be converted to string "undefined"', function (t) {
test('fails with thrown non-error object', function (t) {
var obj = {foo: 'bar'};
var result = ava(function () {
throw undefined; // eslint-disable-line no-throw-literal
throw obj;
}).run();
t.is(result.passed, false);
t.is(result.reason, 'undefined');
t.is(result.reason.actual, obj);
t.is(result.reason.message, 'Non-error thrown with value: { foo: \'bar\' }');
t.is(result.reason.name, 'AssertionError');
t.is(result.reason.operator, 'catch');
t.end();
});

Loading…
Cancel
Save