From 0117313769e0f064589001e38ed7dde7f0711ae8 Mon Sep 17 00:00:00 2001 From: Jan Dockx Date: Sat, 19 Aug 2017 23:59:42 +0200 Subject: [PATCH] deal with UnhandledPromiseRejectionWarning promise now returns the promise, if it is one --- must.js | 3 ++- test/must/promise_test.js | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/must.js b/must.js index 8b8655f..e8eac8f 100644 --- a/must.js +++ b/must.js @@ -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. @@ -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 } /** diff --git a/test/must/promise_test.js b/test/must/promise_test.js index c43e435..625898b 100644 --- a/test/must/promise_test.js +++ b/test/must/promise_test.js @@ -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() + }) }) })