Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Commit

Permalink
Update promise.js
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed Jan 11, 2016
1 parent 047bfe3 commit 3317731
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions lib/ext/promise.js
Expand Up @@ -31,14 +31,22 @@ module.exports = function(should) {
});

/**
* Assert given promise will be fulfilled
* Assert given promise will be fulfilled. Result of assertion is still .thenable and should be handled accordingly.
*
* @name fulfilled
* @memberOf Assertion
* @returns {Promise}
* @category assertion promises
* @example
* (new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilled()
*
* // don't forget to handle async nature
* (new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilled();
*
* // test example with mocha it is possible to return promise
* it('is async', () => {
* return new Promise(resolve => resolve(10))
* .should.be.fulfilled();
* });
*/
Assertion.prototype.fulfilled = function Assertion$fulfilled() {
this.params = {operator: 'to be fulfilled'};
Expand All @@ -60,14 +68,23 @@ module.exports = function(should) {
};

/**
* Assert given promise will be rejected
* Assert given promise will be rejected. Result of assertion is still .thenable and should be handled accordingly.
*
* @name rejected
* @memberOf Assertion
* @category assertion promises
* @returns {Promise}
* @example
* (new Promise(function(resolve, reject) { resolve(10); })).should.not.be.rejected()
*
* // don't forget to handle async nature
* (new Promise(function(resolve, reject) { resolve(10); }))
* .should.not.be.rejected();
*
* // test example with mocha it is possible to return promise
* it('is async', () => {
* return new Promise((resolve, reject) => reject(new Error('boom')))
* .should.be.rejected();
* });
*/
Assertion.prototype.rejected = function() {
this.params = {operator: 'to be rejected'};
Expand All @@ -89,14 +106,24 @@ module.exports = function(should) {
};

/**
* Assert given promise will be fulfilled with some expected value.
* Assert given promise will be fulfilled with some expected value (value compared using .eql).
* Result of assertion is still .thenable and should be handled accordingly.
*
* @name fulfilledWith
* @memberOf Assertion
* @category assertion promises
* @returns {Promise}
* @example
* (new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilledWith(10)
*
* // don't forget to handle async nature
* (new Promise(function(resolve, reject) { resolve(10); }))
* .should.be.fulfilledWith(10);
*
* // test example with mocha it is possible to return promise
* it('is async', () => {
* return new Promise((resolve, reject) => resolve(10))
* .should.be.fulfilledWith(10);
* });
*/
Assertion.prototype.fulfilledWith = function(expectedValue) {
this.params = {operator: 'to be fulfilled'};
Expand All @@ -119,7 +146,8 @@ module.exports = function(should) {
};

/**
* Assert given promise will be rejected with some sort of error. Arguments is the same for Assertion#throw
* Assert given promise will be rejected with some sort of error. Arguments is the same for Assertion#throw.
* Result of assertion is still .thenable and should be handled accordingly.
*
* @name rejectedWith
* @memberOf Assertion
Expand All @@ -128,15 +156,20 @@ module.exports = function(should) {
* @example
*
* function failedPromise() {
* return new Promise(function(resolve, reject) {
* reject(new Error('boom'))
* })
* }
* failedPromise().should.be.rejectedWith(Error)
* failedPromise().should.be.rejectedWith('boom')
* failedPromise().should.be.rejectedWith(/boom/)
* failedPromise().should.be.rejectedWith(Error, { message: 'boom' })
* failedPromise().should.be.rejectedWith({ message: 'boom' })
* return new Promise(function(resolve, reject) {
* reject(new Error('boom'))
* })
* }
* failedPromise().should.be.rejectedWith(Error);
* failedPromise().should.be.rejectedWith('boom');
* failedPromise().should.be.rejectedWith(/boom/);
* failedPromise().should.be.rejectedWith(Error, { message: 'boom' });
* failedPromise().should.be.rejectedWith({ message: 'boom' });
*
* // test example with mocha it is possible to return promise
* it('is async', () => {
* return failedPromise().should.be.rejectedWith({ message: 'boom' });
* });
*/
Assertion.prototype.rejectedWith = function(message, properties) {
this.params = {operator: 'to be rejected'};
Expand Down Expand Up @@ -204,7 +237,9 @@ module.exports = function(should) {
};

/**
* Assert given object is promise and wrap it in PromisedAssertion, which has all properties of Assertion. That means you can chain as with usual Assertion.
* Assert given object is promise and wrap it in PromisedAssertion, which has all properties of Assertion.
* That means you can chain as with usual Assertion.
* Result of assertion is still .thenable and should be handled accordingly.
*
* @name finally
* @memberOf Assertion
Expand All @@ -213,7 +248,14 @@ module.exports = function(should) {
* @returns {PromisedAssertion} Like Assertion, but .then this.obj in Assertion
* @example
*
* (new Promise(function(resolve, reject) { resolve(10); })).should.be.eventually.equal(10)
* (new Promise(function(resolve, reject) { resolve(10); }))
* .should.be.eventually.equal(10);
*
* // test example with mocha it is possible to return promise
* it('is async', () => {
* return new Promise(resolve => resolve(10))
* .should.be.finally.equal(10);
* });
*/
Object.defineProperty(Assertion.prototype, 'finally', {
get: function() {
Expand Down

0 comments on commit 3317731

Please sign in to comment.