Skip to content

Commit

Permalink
deal with UnhandledPromiseRejectionWarning
Browse files Browse the repository at this point in the history
promise now returns the promise, if it is one
  • Loading branch information
jandockx committed Aug 31, 2017
1 parent 43b4ede commit 0117313
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion must.js
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ defineGetter(Must.prototype, "reject", function() {
})

/**
* Assert that an object is a promise.
* Assert that an object is a promise, and returns it.
*
* The determination uses duck typing, i.e., it checks whether the object has
* a `then` and a `catch` method.
Expand Down Expand Up @@ -1213,6 +1213,7 @@ defineGetter(Must.prototype, "reject", function() {
*/
Must.prototype.promise = function() {
this.assert(isPromise(this.actual), isPromiseMsg, { actual: this.actual })
return this.actual
}

/**
Expand Down
22 changes: 20 additions & 2 deletions test/must/promise_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ describe("Must.prototype.promise", function() {
assert.pass(function () { Must(Promise.resolve(42)).be.promise() })
})

it("must pass given a Promise implementation, with a rejected promise", function () {
assert.pass(function () { Must(Promise.reject(new Error())).be.promise() })
it("must pass given a Promise implementation, with a rejected promise (and passes it through)", function(done) {
var p = Promise.reject(new Error())
assert.pass(function() { Must(p).be.promise() })
p.catch(function() { done() }) // deal with UnhandledPromiseRejectionWarning
})

it("passes through a resolved promise", function() {
var p = Promise.resolve(42)
assert(Must(p).be.promise() === p)
})

it("passes through a rejected promise", function(done) {
var rejection = new Error()
var p = Promise.reject(rejection)
var outcome = Must(p).be.promise()
assert(outcome === p)
p.catch(function(err) { // deal with UnhandledPromiseRejectionWarning
assert(err === rejection)
done()
})
})
})

0 comments on commit 0117313

Please sign in to comment.