Skip to content

Commit

Permalink
show output for .throws() when promise is resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 10, 2017
1 parent aa25df5 commit 4a19a96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
16 changes: 11 additions & 5 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function wrapAssertions(callbacks) {
const fail = callbacks.fail;

const noop = () => {};
const makeNoop = () => noop;
const makeRethrow = reason => () => {
throw reason;
};
Expand Down Expand Up @@ -119,14 +118,15 @@ function wrapAssertions(callbacks) {
},

throws(fn, err, message) {
const assertion = 'throws';
let promise;
if (isPromise(fn)) {
promise = fn;
} else if (isObservable(fn)) {
promise = observableToPromise(fn);
} else if (typeof fn !== 'function') {
fail(this, new AssertionError({
assertion: 'throws',
assertion,
improperUsage: true,
message: '`t.throws()` must be called with a function, Promise, or Observable',
values: [formatAssertError.formatWithLabel('Called with:', fn)]
Expand Down Expand Up @@ -174,10 +174,16 @@ 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,
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);
return intermediate;
}

try {
Expand Down
47 changes: 31 additions & 16 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,31 @@ function failsWith(t, fn, subset) {
return;
}

t.is(lastFailure.assertion, subset.assertion);
t.is(lastFailure.message, subset.message);
t.is(lastFailure.name, 'AssertionError');
t.is(lastFailure.operator, subset.operator);
if (subset.statements) {
t.is(lastFailure.statements.length, subset.statements.length);
lastFailure.statements.forEach((s, i) => {
t.is(s[0], subset.statements[i][0]);
t.match(s[1], subset.statements[i][1]);
testFailResults(t, lastFailure, subset);
}

function testFailResults(t, actual, expected) {
t.is(actual.assertion, expected.assertion);
t.is(actual.message, expected.message);
t.is(actual.name, 'AssertionError');
t.is(actual.operator, expected.operator);
if (expected.statements) {
t.is(actual.statements.length, expected.statements.length);
actual.statements.forEach((s, i) => {
t.is(s[0], expected.statements[i][0]);
t.match(s[1], expected.statements[i][1]);
});
} else {
t.same(lastFailure.statements, []);
t.same(actual.statements, []);
}
if (subset.values) {
t.is(lastFailure.values.length, subset.values.length);
lastFailure.values.forEach((s, i) => {
t.is(s.label, subset.values[i].label);
t.match(s.formatted, subset.values[i].formatted);
if (expected.values) {
t.is(actual.values.length, expected.values.length);
actual.values.forEach((s, i) => {
t.is(s.label, expected.values[i].label);
t.match(s.formatted, expected.values[i].formatted);
});
} else {
t.same(lastFailure.values, []);
t.same(actual.values, []);
}
}

Expand Down Expand Up @@ -648,6 +652,17 @@ test('.throws() fails if passed a bad value', t => {
t.end();
});

test('promise .throws() fails when promise is resolved', t => {
return assertions.throws(Promise.resolve('foo'))
.catch(err => {
testFailResults(t, err, {
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(() => {});
Expand Down

0 comments on commit 4a19a96

Please sign in to comment.