Skip to content

Commit

Permalink
Add support for expect(<subject>, expect.it(...))
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 3, 2016
1 parent b0eb87a commit 76c22e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Unexpected.js
Expand Up @@ -1015,6 +1015,12 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
throw new Error('The expect function requires at least one parameter.');
} else if (arguments.length === 1) {
return addAdditionalPromiseMethods(makePromise.resolve(subject), that.expect, subject);
} else if (testDescriptionString && testDescriptionString._expectIt) {
return that.expect.withError(function () {
return testDescriptionString(subject);
}, function (err) {
that.fail(err);
});
}

var executionContext = {
Expand Down Expand Up @@ -1044,12 +1050,20 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
}

var flags = extend({}, assertionRule.flags);
var wrappedExpect = function () {
var subject = arguments[0];
if (arguments.length === 1) {
var wrappedExpect = function (subject, testDescriptionString) {
if (arguments.length === 0) {
throw new Error('The expect function requires at least one parameter.');
} else if (arguments.length === 1) {
return addAdditionalPromiseMethods(makePromise.resolve(subject), wrappedExpect, subject);
} else if (testDescriptionString && testDescriptionString._expectIt) {
wrappedExpect.errorMode = 'nested';
return wrappedExpect.withError(function () {
return testDescriptionString(subject);
}, function (err) {
wrappedExpect.fail(err);
});
}
var testDescriptionString = arguments[1].replace(/\[(!?)([^\]]+)\] ?/g, function (match, negate, flag) {
testDescriptionString = testDescriptionString.replace(/\[(!?)([^\]]+)\] ?/g, function (match, negate, flag) {
return Boolean(flags[flag]) !== Boolean(negate) ? flag + ' ' : '';
}).trim();

Expand Down
18 changes: 18 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -901,6 +901,24 @@ describe('unexpected', function () {
});
});

describe('when followed by an expect.it', function () {
it('should succeed', function () {
clonedExpect('bar', 'when suffixed with foo', expect.it('to equal', 'barfoo'));
});

it('should fail with a diff', function () {
expect(function () {
clonedExpect('bar', 'when suffixed with foo', expect.it('to equal', 'foobar'));
}, 'to throw',
"expected 'bar' when suffixed with foo expect.it('to equal', 'foobar')\n" +
" expected 'barfoo' to equal 'foobar'\n" +
"\n" +
" -barfoo\n" +
" +foobar"
);
});
});

it('should return a promise that has an "and" method', function () {
clonedExpect.addAssertion('<string> to equal foo and when suffixed with bar <assertion?>', function (expect, subject) {
var _len = arguments.length;
Expand Down

0 comments on commit 76c22e7

Please sign in to comment.