-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introducing mocha.throwError for better async error handling #985
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,18 @@ var process = {}; | |
process.exit = function(status){}; | ||
process.stdout = {}; | ||
|
||
var uncaughtExceptionHandlers = []; | ||
|
||
/** | ||
* Remove uncaughtException listener. | ||
*/ | ||
|
||
process.removeListener = function(e){ | ||
process.removeListener = function(e, fn){ | ||
if ('uncaughtException' == e) { | ||
global.onerror = function() {}; | ||
|
||
var indexOfFn = uncaughtExceptionHandlers.indexOf(fn); | ||
if (indexOfFn != -1) { uncaughtExceptionHandlers.splice(indexOfFn, 1); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah, this is the one i fixed. the other one in mocha.js is the generated one. don't bother with that one. |
||
} | ||
}; | ||
|
||
|
@@ -43,6 +48,7 @@ process.on = function(e, fn){ | |
global.onerror = function(err, url, line){ | ||
fn(new Error(err + ' (' + url + ':' + line + ')')); | ||
}; | ||
uncaughtExceptionHandlers.push(fn); | ||
} | ||
}; | ||
|
||
|
@@ -79,6 +85,18 @@ Mocha.Runner.immediately = function(callback) { | |
} | ||
}; | ||
|
||
/** | ||
* Function to allow assertion libraries to throw errors directly into mocha. | ||
* This is useful when running tests in a browser because window.onerror will | ||
* only receive the 'message' attribute of the Error. | ||
*/ | ||
mocha.throwError = function(err) { | ||
uncaughtExceptionHandlers.forEach(function (fn) { | ||
fn(err) ; | ||
}); | ||
throw err; | ||
}; | ||
|
||
/** | ||
* Override ui to ensure that the ui functions are initialized. | ||
* Normally this would happen in Mocha.prototype.loadFiles. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
way too verbose haha
var i =
would do :p