From 430a3520748d455506309f01f96168a9980b880a Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sun, 7 Apr 2013 19:23:32 -0400 Subject: [PATCH] Add a failing test for unhandled rejections and chaining. Introduces `Q.resetUnhandledRejections`, which is necessary for testing. Setting `Q.unhandledReasons.length = 0` like I was doing is dangerous, since it makes the `unhandledReasons` and `unhandledRejections` get out of sync. Re: #238. --- q.js | 6 ++++++ spec/q-spec.js | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/q.js b/q.js index 1645dba4..ad39a899 100644 --- a/q.js +++ b/q.js @@ -750,6 +750,12 @@ function displayUnhandledReasons() { unhandledReasonsDisplayed = true; } +Q.resetUnhandledRejections = function () { + unhandledReasons.length = 0; + unhandledRejections.length = 0; + unhandledReasonsDisplayed = false; +}; + // Show unhandled rejection reasons if Node exits without handling an // outstanding rejection. (Note that Browserify presently produces a process // global without the `EventEmitter` `on` method.) diff --git a/spec/q-spec.js b/spec/q-spec.js index efe595d7..a6e584ac 100644 --- a/spec/q-spec.js +++ b/spec/q-spec.js @@ -2410,13 +2410,23 @@ describe("possible regressions", function () { }); describe("unhandled rejection reporting", function () { - it("doesn't report a resolve, then reject (gh-252)", function () { - Q.unhandledReasons.length = 0; + beforeEach(function () { + Q.resetUnhandledRejections(); + }); + it("doesn't report a resolve, then reject (gh-252)", function () { var deferred = Q.defer(); deferred.resolve(); deferred.reject(); expect(Q.unhandledReasons.length).toEqual(0); }); + + it("doesn't report when you chain off a rejection", function () { + Q.reject("this will be handled").get("property").fail(function () { + // now it should be handled. + }); + + expect(Q.unhandledReasons.length).toEqual(0); + }); });