Report unhandled rejections as globalErrors.
Extend existing support for uncaughtExceptions to unhandledRejections now that many tests are async.
This commit is contained in:
@@ -78,7 +78,7 @@ describe("GlobalErrors", function() {
|
||||
errors.uninstall();
|
||||
});
|
||||
|
||||
it("works in node.js", function() {
|
||||
it("reports uncaughtException in node.js", function() {
|
||||
var fakeGlobal = {
|
||||
process: {
|
||||
on: jasmine.createSpy('process.on'),
|
||||
@@ -107,4 +107,34 @@ describe("GlobalErrors", function() {
|
||||
expect(fakeGlobal.process.removeListener).toHaveBeenCalledWith('uncaughtException', addedListener);
|
||||
expect(fakeGlobal.process.on).toHaveBeenCalledWith('uncaughtException', 'foo');
|
||||
});
|
||||
|
||||
it("reports unhandledRejection in node.js", function() {
|
||||
var fakeGlobal = {
|
||||
process: {
|
||||
on: jasmine.createSpy('process.on'),
|
||||
removeListener: jasmine.createSpy('process.removeListener'),
|
||||
listeners: jasmine.createSpy('process.listeners').and.returnValue(['foo']),
|
||||
removeAllListeners: jasmine.createSpy('process.removeAllListeners')
|
||||
}
|
||||
},
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
expect(fakeGlobal.process.on).toHaveBeenCalledWith('unhandledRejection', jasmine.any(Function));
|
||||
expect(fakeGlobal.process.listeners).toHaveBeenCalledWith('unhandledRejection');
|
||||
expect(fakeGlobal.process.removeAllListeners).toHaveBeenCalledWith('unhandledRejection');
|
||||
|
||||
errors.pushListener(handler);
|
||||
|
||||
var addedListener = fakeGlobal.process.on.calls.argsFor(0)[1];
|
||||
addedListener(new Error('bar'));
|
||||
|
||||
expect(handler).toHaveBeenCalledWith(new Error('bar'));
|
||||
|
||||
errors.uninstall();
|
||||
|
||||
expect(fakeGlobal.process.removeListener).toHaveBeenCalledWith('unhandledRejection', addedListener);
|
||||
expect(fakeGlobal.process.on).toHaveBeenCalledWith('unhandledRejection', 'foo');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user