Merge branch 'handle-rejection' of https://github.com/johnjbarton/jasmine
- Merges #1521 from @johnjbarton
This commit is contained in:
@@ -2750,18 +2750,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() {
|
this.install = function install() {
|
||||||
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
|
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
|
||||||
var originalHandlers = global.process.listeners('uncaughtException');
|
this.installOne_('uncaughtException');
|
||||||
global.process.removeAllListeners('uncaughtException');
|
this.installOne_('unhandledRejection');
|
||||||
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]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
var originalHandler = global.onerror;
|
var originalHandler = global.onerror;
|
||||||
global.onerror = onerror;
|
global.onerror = onerror;
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ describe("GlobalErrors", function() {
|
|||||||
errors.uninstall();
|
errors.uninstall();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("works in node.js", function() {
|
it("reports uncaughtException in node.js", function() {
|
||||||
var fakeGlobal = {
|
var fakeGlobal = {
|
||||||
process: {
|
process: {
|
||||||
on: jasmine.createSpy('process.on'),
|
on: jasmine.createSpy('process.on'),
|
||||||
@@ -107,4 +107,34 @@ describe("GlobalErrors", function() {
|
|||||||
expect(fakeGlobal.process.removeListener).toHaveBeenCalledWith('uncaughtException', addedListener);
|
expect(fakeGlobal.process.removeListener).toHaveBeenCalledWith('uncaughtException', addedListener);
|
||||||
expect(fakeGlobal.process.on).toHaveBeenCalledWith('uncaughtException', 'foo');
|
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() {
|
this.install = function install() {
|
||||||
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
|
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
|
||||||
var originalHandlers = global.process.listeners('uncaughtException');
|
this.installOne_('uncaughtException');
|
||||||
global.process.removeAllListeners('uncaughtException');
|
this.installOne_('unhandledRejection');
|
||||||
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]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
var originalHandler = global.onerror;
|
var originalHandler = global.onerror;
|
||||||
global.onerror = onerror;
|
global.onerror = onerror;
|
||||||
|
|||||||
Reference in New Issue
Block a user