Browse Source

Close #259 PR: print the first assertion failure of each test, instead of the last. Fixes #121, #220. Fixes #121, Fixes #220

babel-plugin-for-integration-tests
James Talmage 9 years ago
committed by Sindre Sorhus
parent
commit
eb9b339029
  1. 35
      lib/test.js
  2. 2
      readme.md
  3. 30
      test/test.js

35
lib/test.js

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

2
readme.md

@ -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.

30
test/test.js

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

Loading…
Cancel
Save