Skip to content

Commit 000826e

Browse files
committed
assert: make t.throws() accept async function as parameter.
Refs: #1371
1 parent bcb77fc commit 000826e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/assert.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ function wrapAssertions(callbacks) {
182182
coreAssertThrowsErrorArg = err;
183183
}
184184

185+
let maybePromise;
185186
const test = (fn, stack) => {
186187
let actual;
187188
let threw = false;
188189
try {
189190
coreAssert.throws(() => {
190191
try {
191-
fn();
192+
maybePromise = fn();
192193
} catch (err) {
193194
actual = err;
194195
threw = true;
@@ -208,7 +209,7 @@ function wrapAssertions(callbacks) {
208209
}
209210
};
210211

211-
if (promise) {
212+
const handlePromise = promise => {
212213
// Record stack before it gets lost in the promise chain.
213214
const stack = getStack();
214215
const intermediate = promise.then(value => {
@@ -222,13 +223,25 @@ function wrapAssertions(callbacks) {
222223
pending(this, intermediate);
223224
// Don't reject the returned promise, even if the assertion fails.
224225
return intermediate.catch(noop);
226+
};
227+
228+
if (promise) {
229+
return handlePromise.call(this, promise);
225230
}
226231

227232
try {
228233
const retval = test(fn);
229234
pass(this);
230235
return retval;
231236
} catch (err) {
237+
if (maybePromise) {
238+
if (isPromise(maybePromise)) {
239+
return handlePromise.call(this, maybePromise);
240+
}
241+
if (isObservable(maybePromise)) {
242+
return handlePromise.call(this, observableToPromise(maybePromise));
243+
}
244+
}
232245
fail(this, err);
233246
}
234247
},

test/assert.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ test('.throws() returns the rejection reason of promise', t => {
678678
});
679679
});
680680

681+
test('.throws() returns the rejection reason of function that return rejected `Promise`', t => {
682+
const expected = new Error();
683+
684+
return assertions.throws(() => {
685+
return Promise.reject(expected);
686+
}).then(actual => {
687+
t.is(actual, expected);
688+
t.end();
689+
});
690+
});
691+
681692
test('.throws() fails if passed a bad value', t => {
682693
failsWith(t, () => {
683694
assertions.throws('not a function');

0 commit comments

Comments
 (0)