Skip to content

Commit feef1bd

Browse files
committed
print the first assertion failure of each test, instead of the last. Fixes #121, #220
1 parent 293acf2 commit feef1bd

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

lib/test.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function Test(title, fn) {
2626
this.assertCount = 0;
2727
this.planCount = null;
2828
this.duration = null;
29+
this.assertError = undefined;
2930

3031
// test type, can be: test, hook, eachHook
3132
this.type = 'test';
@@ -67,20 +68,29 @@ Object.keys(assert).forEach(function (el) {
6768
if (isPromise(fn)) {
6869
return Promise.resolve(fn)
6970
.catch(function (err) {
70-
self.assertError = err;
71+
self._setAssertError(err);
7172
})
7273
.finally(function () {
7374
self._assert();
7475
});
7576
}
7677
} catch (err) {
77-
this.assertError = err;
78+
this._setAssertError(err);
7879
}
7980

8081
this._assert();
8182
};
8283
});
8384

85+
Test.prototype._setAssertError = function (err) {
86+
if (this.assertError === undefined) {
87+
if (err === undefined) {
88+
err = 'undefined';
89+
}
90+
this.assertError = err;
91+
}
92+
};
93+
8494
// Workaround for power-assert
8595
// `t` must be capturable for decorated assert output
8696
Test.prototype._capt = assert._capt;
@@ -118,7 +128,7 @@ Test.prototype.run = function () {
118128
try {
119129
ret = this.fn(this);
120130
} catch (err) {
121-
this.assertError = err;
131+
this._setAssertError(err);
122132
this.exit();
123133
}
124134

@@ -132,11 +142,11 @@ Test.prototype.run = function () {
132142
}
133143
})
134144
.catch(function (err) {
135-
self.assertError = new assert.AssertionError({
145+
self._setAssertError(new assert.AssertionError({
136146
actual: err,
137147
message: 'Promise rejected → ' + err,
138148
operator: 'promise'
139-
});
149+
}));
140150

141151
self.exit();
142152
});
@@ -147,11 +157,11 @@ Test.prototype.run = function () {
147157

148158
Test.prototype.end = function (err) {
149159
if (err) {
150-
this.assertError = new assert.AssertionError({
160+
this._setAssertError(new assert.AssertionError({
151161
actual: err,
152162
message: 'Callback called with an error → ' + err,
153163
operator: 'callback'
154-
});
164+
}));
155165

156166
this.exit();
157167
return;
@@ -174,13 +184,13 @@ Test.prototype.exit = function () {
174184
// stop infinite timer
175185
clearTimeout(this._timeout);
176186

177-
if (!this.assertError && this.planCount !== null && this.planCount !== this.assertCount) {
178-
this.assertError = new assert.AssertionError({
187+
if (this.assertError === undefined && this.planCount !== null && this.planCount !== this.assertCount) {
188+
this._setAssertError(new assert.AssertionError({
179189
actual: this.assertCount,
180190
expected: this.planCount,
181191
message: 'Assertion count does not match planned',
182192
operator: 'plan'
183-
});
193+
}));
184194

185195
this.assertError.stack = this.planStack;
186196
}
@@ -189,7 +199,7 @@ Test.prototype.exit = function () {
189199
this.ended = true;
190200

191201
setImmediate(function () {
192-
if (self.assertError) {
202+
if (self.assertError !== undefined) {
193203
self.promise.reject(self.assertError);
194204
return;
195205
}

test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,33 @@ test('wait for test to end', function (t) {
312312
avaTest.pass();
313313
}, 1234);
314314
});
315+
316+
test('promise is rejected with the first assertError', function (t) {
317+
ava(function (a) {
318+
a.plan(2);
319+
a.is(1, 2);
320+
a.is(3, 4);
321+
}).run().catch(function (err) {
322+
t.is(err.actual, 1);
323+
t.is(err.expected, 2);
324+
t.end();
325+
});
326+
});
327+
328+
test('promise is rejected with thrown falsie value', function (t) {
329+
ava(function () {
330+
throw 0; // eslint-disable-line no-throw-literal
331+
}).run().catch(function (err) {
332+
t.equal(err, 0);
333+
t.end();
334+
});
335+
});
336+
337+
test('throwing undefined will be converted to string "undefined"', function (t) {
338+
ava(function () {
339+
throw undefined; // eslint-disable-line no-throw-literal
340+
}).run().catch(function (err) {
341+
t.equal(err, 'undefined');
342+
t.end();
343+
});
344+
});

0 commit comments

Comments
 (0)