Skip to content

Commit

Permalink
Merge 7a84b86 into bdf7b0e
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 30, 2015
2 parents bdf7b0e + 7a84b86 commit af6435d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,17 +1107,23 @@ module.exports = function (expect) {
this.errorMode = 'nested';
var that = this;
return subject.then(function (value) {
return that.shift(value, 0);
}, function (err) {
if (typeof nextAssertion === 'string') {
that.argsOutput = function (output) {
output.error(nextAssertion);
var rest = that.args.slice(1);
if (rest.length > 0) {
output.sp().appendItems(rest, ', ');
}
};
if (expect.findTypeOf(nextAssertion).is('expect.it')) {
// Force a failing expect.it error message to be property nested instead of replacing the default error message:
return expect.promise(function () {
return that.shift(value, 0);
}).caught(expect.fail);
} else {
return that.shift(value, 0);
}
}, function (err) {
// typeof nextAssertion === 'string' because expect.it is handled by the above (and shift only supports those two):
that.argsOutput = function (output) {
output.error(nextAssertion);
var rest = that.args.slice(1);
if (rest.length > 0) {
output.sp().appendItems(rest, ', ');
}
};
expect.fail(function (output) {
output.appendInspected(subject).sp().text('unexpectedly rejected');
if (typeof err !== 'undefined') {
Expand Down
46 changes: 46 additions & 0 deletions test/unexpected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,14 @@ describe('unexpected', function () {
}), 'when fulfilled', 'to satisfy', { foo: 'bar' });
});

it('should support expect.it', function () {
return expect(new Promise(function (resolve, reject) {
setTimeout(function () {
resolve({ foo: 'bar' });
}, 0);
}), 'when fulfilled', expect.it('to have property', 'foo', 'bar'));
});

it('should fail when the promise is rejected', function () {
return expect(
expect(new Promise(function (resolve, reject) {
Expand All @@ -1421,6 +1429,28 @@ describe('unexpected', function () {
);
});

it('should fail when the promise is rejected with a value of undefined', function () {
return expect(
expect(new Promise(function (resolve, reject) {
setTimeout(reject, 0);
}), 'when fulfilled', 'to satisfy', { foo: 'baz' }),
'to be rejected with',
"expected Promise when fulfilled to satisfy { foo: 'baz' }\n" +
" Promise unexpectedly rejected"
);
});

it('should fail when the promise is rejected with a value of undefined', function () {
return expect(
expect(new Promise(function (resolve, reject) {
setTimeout(reject, 0);
}), 'when fulfilled', 'to be truthy'),
'to be rejected with',
"expected Promise when fulfilled to be truthy\n" +
" Promise unexpectedly rejected"
);
});

it('should fail when the next assertion fails', function () {
return expect(
expect(new Promise(function (resolve, reject) {
Expand All @@ -1439,6 +1469,22 @@ describe('unexpected', function () {
" }"
);
});

it('should fail with the right error message when the next assertion is an expect.it that fails', function () {
return expect(
expect(new Promise(function (resolve, reject) {
setTimeout(function () {
resolve({ foo: 'bar' });
}, 0);
}), 'when fulfilled', expect.it('to have property', 'foo', 'quux')),
'to be rejected with',
"expected Promise when fulfilled expect.it('to have property', 'foo', 'quux')\n" +
" expected { foo: 'bar' } to have property 'foo' with a value of 'quux'\n" +
"\n" +
" -bar\n" +
" +quux"
);
});
});

describe('"when rejected" adverbial assertion', function () {
Expand Down

0 comments on commit af6435d

Please sign in to comment.