-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch unhandled promise rejections during tests
Listen to the unhandledRejection event when running tests, catching promises who got rejected without being cared for, and failing the test in which said promises were left unhandled. This is only part of handling unhandled rejections, some things not yet included are: - Handling unhandled rejections outside spec tests (e.g. testhooks) - Making it work in a browser - Amend error messages which reference unhandled rejections - Testing the new signature of the `Runner.prototype.uncaught` method If this commit looks nice enough, a future commit will amend these shortcomings.
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
function rejectWith(err) { | ||
return new Promise(function (resolve, reject) { | ||
reject(err); | ||
}); | ||
} | ||
|
||
// We wish to test unhandled rejections, which happen after a tick at the | ||
// earliest, so we need a way some way to signal mocha our tests are async. One | ||
// way is to accept the `done` parameter without ever using it. Another is | ||
// returning a promise which never resolves. Since this tests promise handling, | ||
// why don't we go all the way with the promises? | ||
var unresolvedPromise = new Promise(function () {}); | ||
|
||
it('fails when faced with an unhandled rejection', function () { | ||
rejectWith(new Error('rejection')); | ||
|
||
return unresolvedPromise; | ||
}); | ||
|
||
it('fails exactly once when a global promise is rejected first', function () { | ||
setTimeout(function () { | ||
rejectWith(new Error('global error')); | ||
}, 0); | ||
|
||
return unresolvedPromise; | ||
}); | ||
|
||
it('fails exactly once when a global promise is rejected second', function () { | ||
setTimeout(function () { | ||
rejectWith(new Error('test error')); | ||
}, 0); | ||
|
||
setTimeout(function () { | ||
rejectWith(new Error('global error')); | ||
}, 0); | ||
|
||
return unresolvedPromise; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters