Skip to content

Commit

Permalink
Add a failing test for unhandled rejections and chaining.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
domenic committed May 10, 2013
1 parent c29ca54 commit 430a352
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
14 changes: 12 additions & 2 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 430a352

Please sign in to comment.