Report unhandled rejections as globalErrors.

Extend existing support for uncaughtExceptions to unhandledRejections now that many tests are async.
This commit is contained in:
johnjbarton
2018-02-21 07:23:20 -08:00
parent 85b2a8c6c4
commit f1ebe05f1d
2 changed files with 52 additions and 11 deletions

View File

@@ -13,18 +13,29 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
}
};
this.originalHandlers = {};
this.installOne_ = function installOne_(errorType) {
this.originalHandlers[errorType] = global.process.listeners(errorType);
global.process.removeAllListeners(errorType);
global.process.on(errorType, onerror);
this.uninstall = function uninstall() {
var errorTypes = Object.keys(this.originalHandlers);
for (var iType = 0; iType < errorTypes.length; iType++) {
var errorType = errorTypes[iType];
global.process.removeListener(errorType, onerror);
for (var i = 0; i < this.originalHandlers[errorType].length; i++) {
global.process.on(errorType, this.originalHandlers[errorType][i]);
}
delete this.originalHandlers[errorType];
}
};
};
this.install = function install() {
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
var originalHandlers = global.process.listeners('uncaughtException');
global.process.removeAllListeners('uncaughtException');
global.process.on('uncaughtException', onerror);
this.uninstall = function uninstall() {
global.process.removeListener('uncaughtException', onerror);
for (var i = 0; i < originalHandlers.length; i++) {
global.process.on('uncaughtException', originalHandlers[i]);
}
};
this.installOne_('uncaughtException');
this.installOne_('unhandledRejection');
} else {
var originalHandler = global.onerror;
global.onerror = onerror;