Skip to content

Commit

Permalink
Fix #8: problems with .deep.equal.
Browse files Browse the repository at this point in the history
In the `deep` case, `equal` called `eql`, another Chai asserter. However, Chai as Promised generally did not take into account Chai asserters calling other Chai asserters. Since Chai as Promise had created assertion-promise–returning versions of all Chai asserters, some special care is needed in such situations.
  • Loading branch information
domenic committed Nov 4, 2012
1 parent 0fb8cbc commit 0d434ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/chai-as-promised.js
Expand Up @@ -30,6 +30,10 @@
}
}

function isAssertionPromise(candidate) {
return candidate && typeof candidate.then === "function" && typeof candidate.assert === "function";
}

function property(name, asserter) {
utils.addProperty(Assertion.prototype, name, function () {
assertIsAboutPromise(this);
Expand Down Expand Up @@ -240,9 +244,16 @@
utils.flag(currentAssertionPromise, "object", fulfillmentValue);

// Perform the actual asserter action.
doAsserter();
var result = doAsserter();

// If the asserter calls another asserter, then the result will be an assertion-promise, since all asserters
// on `currentAssertionPromise` return assertion-promises now. In that case, we should return that promise
// to signal that we need to wait until it's resolved before getting a result.
if (isAssertionPromise(result)) {
return result;
}

// Return the fulfillmentValue for future assertions of the new assertion-promise to act upon.
// Otherwise, return the `fulfillmentValue` for future assertions of the new assertion-promise to act upon.
return fulfillmentValue;
});
return makeAssertionPromise(promiseToDoAsserter, currentAssertionPromise);
Expand Down Expand Up @@ -290,7 +301,7 @@
var args = arguments;

return makeAssertionPromiseToDoAsserter(assertionPromise, baseAssertion, function () {
propertyDescriptor.value.apply(assertionPromise, args);
return propertyDescriptor.value.apply(assertionPromise, args);
});
});
} else if (typeof propertyDescriptor.get === "function") {
Expand All @@ -313,18 +324,18 @@
var args = arguments;

return makeAssertionPromiseToDoAsserter(assertionPromise, baseAssertion, function () {
propertyDescriptor.get().apply(assertionPromise, args);
return propertyDescriptor.get().apply(assertionPromise, args);
});
},
function () {
propertyDescriptor.get.call(assertionPromise);
return propertyDescriptor.get.call(assertionPromise);
}
);
} else {
// Case 2B: pure property case
utils.addProperty(assertionPromise, asserterName, function () {
return makeAssertionPromiseToDoAsserter(assertionPromise, baseAssertion, function () {
propertyDescriptor.get.call(assertionPromise);
return propertyDescriptor.get.call(assertionPromise);
});
});
}
Expand Down
8 changes: 8 additions & 0 deletions test/fulfillmentValues.coffee
Expand Up @@ -105,6 +105,14 @@
shouldFail -> promise.should.eventually.equal(foo: "bar")
describe ".eventually.eql({ foo: 'bar' })", ->
shouldPass -> promise.should.eventually.eql(foo: "bar")
describe ".eventually.deep.equal({ foo: 'bar' })", ->
shouldPass -> promise.should.eventually.deep.equal(foo: "bar")
describe ".eventually.not.deep.equal({ foo: 'bar' })", ->
shouldFail -> promise.should.eventually.not.deep.equal(foo: "bar")
describe ".eventually.deep.equal({ baz: 'quux' })", ->
shouldFail -> promise.should.eventually.deep.equal(baz: "quux")
describe ".eventually.not.deep.equal({ baz: 'quux' })", ->
shouldPass -> promise.should.eventually.not.deep.equal(baz: "quux")
describe ".become({ foo: 'bar' })", ->
shouldPass -> promise.should.become(foo: "bar")
describe ".not.become({ foo: 'bar' })", ->
Expand Down

0 comments on commit 0d434ae

Please sign in to comment.