chai-as-promised
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -56,3 +56,3 @@ (function (chaiAsPromised) { | ||
// Don't return the original value, i.e. swallow fulfillment values so that `.then(done, done)` works. | ||
return value; | ||
}.bind(this), | ||
@@ -115,8 +115,3 @@ function (reason) { | ||
// Use `Object.create` to ensure we get an extensible promise, so we can add `with`. Q promises, for example, | ||
// are non-extensible. | ||
var transformedPromise = Object.create(this.obj.then(onOriginalFulfilled, onOriginalRejected)); | ||
// Augment the transformed promise with a `with` method that performs further transformations. | ||
transformedPromise.with = function (Constructor, message) { | ||
var withMethod = function (Constructor, message) { | ||
var desiredReason = null; | ||
@@ -223,2 +218,5 @@ | ||
var transformedPromise = promiseWithAsserters(this.obj.then(onOriginalFulfilled, onOriginalRejected), this); | ||
Object.defineProperty(transformedPromise, "with", { enumerable: true, configurable: true, value: withMethod }); | ||
return transformedPromise; | ||
@@ -250,5 +248,16 @@ }; | ||
// Use `Object.create` to ensure we get an extensible promise, so we can add `with`. Q promises, for example, | ||
// are non-extensible. | ||
var augmentedPromise = Object.create(promise); | ||
var asserterNames = Object.getOwnPropertyNames(Assertion.prototype); | ||
asserterNames.forEach(function (asserterName) { | ||
if (["fulfilled", "rejected", "broken", "eventually", "become"].indexOf(asserterName) !== -1) { | ||
Object.defineProperty(augmentedPromise, asserterName, { | ||
get: function () { | ||
throw new Error("Cannot use Chai as Promised asserters more than once in an assertion."); | ||
} | ||
}); | ||
return; | ||
} | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(Assertion.prototype, asserterName); | ||
@@ -255,0 +264,0 @@ |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenicdenicola.com)", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -104,2 +104,18 @@ Chai Assertions for Working with Promises | ||
### Progress Callbacks | ||
Chai as Promised does not have any intrinsic support for testing promise progress callbacks. The properties you would | ||
want to test are probably much better suited to a library like [Sinon.JS][sinon], perhaps in conjunction with | ||
[Sinon–Chai][sinon-chai]: | ||
```javascript | ||
var progressSpy = sinon.spy(); | ||
return promise.then(null, null, progressSpy).then(function () { | ||
progressSpy.should.have.been.calledWith("33%"); | ||
progressSpy.should.have.been.calledWith("67%"); | ||
progressSpy.should.have.been.calledThrice; | ||
}); | ||
``` | ||
### Working with Non-Promise–Friendly Test Runners | ||
@@ -125,4 +141,4 @@ | ||
There's another form of notify which is useful in certain situations, like doing assertions after a promise is complete. | ||
For example: | ||
There's another form of `notify` which is useful in certain situations, like doing assertions after a promise is | ||
complete. For example: | ||
@@ -143,2 +159,18 @@ ```javascript | ||
Another example of where this can be useful is when performing assertions on multiple promises: | ||
```javascript | ||
it("should all be well", function (done) { | ||
Q.all([ | ||
promiseA.should.become("happy"), | ||
promiseB.should.eventually.have.property("fun times"), | ||
promiseC.should.be.rejected.with(TypeError, "only joyful types are allowed") | ||
]).should.notify(done); | ||
}); | ||
``` | ||
This will pass any failures of the individual promise assertions up to the test framework, instead of wrapping them in | ||
an `"expected promise to be fulfilled…"` message as would happen if you did | ||
`Q.all([…]).should.be.fulfilled.and.notify(done)`. | ||
## Installation and Usage | ||
@@ -192,1 +224,3 @@ | ||
[amd]: https://github.com/amdjs/amdjs-api/wiki/AMD | ||
[sinon]: http://sinonjs.org/ | ||
[sinon-chai]: https://github.com/domenic/sinon-chai |
Sorry, the diff of this file is not supported yet
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
46708
304
223