diff --git a/lib/assert.js b/lib/assert.js index 28b8ae09c..b15568109 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -41,7 +41,6 @@ function wrapAssertions(callbacks) { const fail = callbacks.fail; const noop = () => {}; - const makeNoop = () => noop; const makeRethrow = reason => () => { throw reason; }; @@ -174,7 +173,14 @@ function wrapAssertions(callbacks) { if (promise) { // Record stack before it gets lost in the promise chain. const stack = getStack(); - const intermediate = promise.then(makeNoop, makeRethrow).then(fn => test(fn, stack)); + const intermediate = promise.then(value => { + throw new AssertionError({ + assertion: 'throws', + message: 'Expected promise to be rejected, but it was resolved instead', + values: [formatAssertError.formatWithLabel('Resolved with:', value)] + }); + }, reason => test(makeRethrow(reason), stack)); + pending(this, intermediate); // Don't reject the returned promise, even if the assertion fails. return intermediate.catch(noop); diff --git a/test/assert.js b/test/assert.js index 64bb9bee9..67d8bb7bd 100644 --- a/test/assert.js +++ b/test/assert.js @@ -12,16 +12,18 @@ const assertions = assert.wrapAssertions({ lastPassed = true; }, - pending() {}, + pending(_, promise) { + promise.catch(err => { + lastFailure = err; + }); + }, fail(_, error) { lastFailure = error; } }); -function failsWith(t, fn, subset) { - lastFailure = null; - fn(); +function assertFailure(t, subset) { if (!lastFailure) { t.fail('Expected assertion to fail'); return; @@ -51,6 +53,19 @@ function failsWith(t, fn, subset) { } } +function failsWith(t, fn, subset) { + lastFailure = null; + fn(); + assertFailure(t, subset); +} + +function eventuallyFailsWith(t, promise, subset) { + lastFailure = null; + return promise.then(() => { + assertFailure(t, subset); + }); +} + function fails(t, fn) { lastFailure = null; fn(); @@ -648,6 +663,14 @@ test('.throws() fails if passed a bad value', t => { t.end(); }); +test('promise .throws() fails when promise is resolved', t => { + return eventuallyFailsWith(t, assertions.throws(Promise.resolve('foo')), { + assertion: 'throws', + message: 'Expected promise to be rejected, but it was resolved instead', + values: [{label: 'Resolved with:', formatted: formatValue('foo')}] + }); +}); + test('.notThrows()', t => { passes(t, () => { assertions.notThrows(() => {});