chai-as-promised
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -36,5 +36,9 @@ (function (chaiAsPromised) { | ||
function notify(promise, callback) { | ||
return promise.then(function () { callback(); }, callback); | ||
} | ||
function addNotifyMethod(extensiblePromise) { | ||
extensiblePromise.notify = function (callback) { | ||
return extensiblePromise.then(function () { callback(); }, callback); | ||
return notify(extensiblePromise, callback); | ||
}; | ||
@@ -284,2 +288,6 @@ } | ||
method("notify", function (callback) { | ||
return notify(this.obj, callback); | ||
}); | ||
// Now use the Assertion framework to build an `assert` interface. | ||
@@ -300,4 +308,4 @@ var originalAssertMethods = Object.getOwnPropertyNames(assert).filter(function (propName) { | ||
var promise = (new Assertion(promise, message)).to.be.rejected; | ||
return toTestAgainst ? promise.with(toTestAgainst) : promise; | ||
var shouldBeRejectedPromise = (new Assertion(promise, message)).to.be.rejected; | ||
return toTestAgainst ? shouldBeRejectedPromise.with(toTestAgainst) : shouldBeRejectedPromise; | ||
}; | ||
@@ -304,0 +312,0 @@ |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenicdenicola.com)", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -33,4 +33,6 @@ Chai Assertions for Working with Promises | ||
## How to Use: `should`/`expect` Interface | ||
## How to Use | ||
### `should`/`expect` Interface | ||
The most powerful extension provided by Chai as Promised is the `eventually` property. With it, you can transform any | ||
@@ -52,4 +54,4 @@ existing Chai assertion into one that acts on a promise: | ||
There are also a few promise-specific extensions, grouped here as synonymic blocks (with the usual `expect` equivalents | ||
also available): | ||
There are also a few promise-specific extensions, grouped here as synonymic blocks (with the usual `expect` | ||
equivalents): | ||
@@ -71,3 +73,3 @@ ```javascript | ||
## How to Use: `assert` Interface | ||
### `assert` Interface | ||
@@ -89,7 +91,7 @@ As with the `should`/`expect` interface, Chai as Promised provides an `eventually` extender to `chai.assert`, allowing | ||
return assert.eventually.deepEqual(promise, "foo"); | ||
return assert.becomes(promise, "foo"); | ||
return assert.eventually.deepEqual(promise, "foo", "optional message"); | ||
return assert.becomes(promise, "foo", "optional message"); | ||
return assert.eventually.notDeepEqual(promise, "foo"); | ||
return assert.doesNotBecome(promise, "foo"); | ||
return assert.eventually.notDeepEqual(promise, "foo", "optional message"); | ||
return assert.doesNotBecome(promise, "foo", "optional message"); | ||
@@ -106,2 +108,39 @@ return assert.isRejected(promise, "optional message"); | ||
### Working with Non-Promise–Friendly Test Runners | ||
As mentioned, many test runners (\*cough\* [mocha][mocha-makes-me-sad] \*cough\*) don't support the nice `return` style | ||
shown above. Instead, they take a callback indicating when the asynchronous test run is over. Chai as Promised adapts to | ||
this situation with the `notify` method, like so: | ||
```javascript | ||
it("should be fulfilled", function (done) { | ||
promise.should.be.fulfilled.and.notify(done); | ||
}); | ||
it("should be rejected", function (done) { | ||
otherPromise.should.be.rejected.and.notify(done); | ||
}); | ||
``` | ||
In these examples, if the conditions are not met, the test runner will receive an error of the form `"expected promise | ||
to be fulfilled but it was rejected with [Error: error message]"`, or `"expected promise to be rejected but it was | ||
fulfilled."` | ||
There's another form of notify which is useful in certain situations, like doing assertions after a promise is complete. | ||
For example: | ||
```javascript | ||
it("should change the state", function (done) { | ||
otherState.should.equal("before"); | ||
promise.should.be.fulfilled.then(function () { | ||
otherState.should.equal("after"); | ||
}).should.notify(done); | ||
}); | ||
``` | ||
Notice how `.notify(done)` is hanging directly off of `.should`, instead of appearing after a promise assertion. This | ||
indicates to Chai as Promised that it should pass fulfillment or rejection directly through to the testing framework. | ||
Thus, the above code will fail with a Chai as Promised error (`"expected promise to be fulfilled…"`) if `promise` is | ||
rejected, but will fail with a simple Chai error (`expected "before" to equal "after"`) if `otherState` does not change. | ||
## Installation and Usage | ||
@@ -150,4 +189,6 @@ | ||
[chai]: http://chaijs.com/ | ||
[mocha]: http://visionmedia.github.com/mocha/ | ||
[mocha-makes-me-sad]: https://github.com/visionmedia/mocha/pull/329 | ||
[uncommonjs]: http://kriskowal.github.com/uncommonjs/tests/specification | ||
[fixturedemo]: https://github.com/domenic/promised-chai/tree/master/test/ | ||
[fixturedemo]: https://github.com/domenic/chai-as-promised/tree/master/test/ | ||
[amd]: https://github.com/amdjs/amdjs-api/wiki/AMD |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43655
296
189