Collect unhandled exceptions and pass them to the current runnable
Fixes #529 Fixes #937
This commit is contained in:
@@ -79,6 +79,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
|
||||
j$.DiffBuilder = jRequire.DiffBuilder(j$);
|
||||
j$.NullDiffBuilder = jRequire.NullDiffBuilder(j$);
|
||||
j$.ObjectPath = jRequire.ObjectPath(j$);
|
||||
j$.GlobalErrors = jRequire.GlobalErrors(j$);
|
||||
|
||||
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
||||
|
||||
@@ -581,6 +582,8 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
'specDone'
|
||||
]);
|
||||
|
||||
var globalErrors = new j$.GlobalErrors();
|
||||
|
||||
this.specFilter = function() {
|
||||
return true;
|
||||
};
|
||||
@@ -728,6 +731,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
options.clearStack = options.clearStack || clearStack;
|
||||
options.timeout = {setTimeout: realSetTimeout, clearTimeout: realClearTimeout};
|
||||
options.fail = self.fail;
|
||||
options.globalErrors = globalErrors;
|
||||
|
||||
new j$.QueueRunner(options).execute();
|
||||
};
|
||||
@@ -791,9 +795,11 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
|
||||
currentlyExecutingSuites.push(topSuite);
|
||||
|
||||
globalErrors.install();
|
||||
processor.execute(function() {
|
||||
clearResourcesForRunnable(topSuite.id);
|
||||
currentlyExecutingSuites.pop();
|
||||
globalErrors.uninstall();
|
||||
|
||||
reporter.jasmineDone({
|
||||
order: order,
|
||||
@@ -1912,6 +1918,52 @@ getJasmineRequireObj().formatErrorMsg = function() {
|
||||
return generateErrorMsg;
|
||||
};
|
||||
|
||||
getJasmineRequireObj().GlobalErrors = function(j$) {
|
||||
function GlobalErrors(global) {
|
||||
var handlers = [];
|
||||
global = global || j$.getGlobal();
|
||||
|
||||
var onerror = function onerror() {
|
||||
var handler = handlers[handlers.length - 1];
|
||||
handler.apply(null, Array.prototype.slice.call(arguments, 0));
|
||||
};
|
||||
|
||||
this.uninstall = function noop() {};
|
||||
|
||||
this.install = function install() {
|
||||
if (global.process && 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]);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
var originalHandler = global.onerror;
|
||||
global.onerror = onerror;
|
||||
|
||||
this.uninstall = function uninstall() {
|
||||
global.onerror = originalHandler;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
this.pushListener = function pushListener(listener) {
|
||||
handlers.push(listener);
|
||||
};
|
||||
|
||||
this.popListener = function popListener() {
|
||||
handlers.pop();
|
||||
};
|
||||
}
|
||||
|
||||
return GlobalErrors;
|
||||
};
|
||||
|
||||
getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||
return function DiffBuilder() {
|
||||
var path = new j$.ObjectPath(),
|
||||
@@ -3237,6 +3289,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
this.userContext = attrs.userContext || {};
|
||||
this.timeout = attrs.timeout || {setTimeout: setTimeout, clearTimeout: clearTimeout};
|
||||
this.fail = attrs.fail || function() {};
|
||||
this.globalErrors = attrs.globalErrors || { pushListener: function() {}, popListener: function() {} };
|
||||
}
|
||||
|
||||
QueueRunner.prototype.execute = function() {
|
||||
@@ -3273,8 +3326,13 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
var clearTimeout = function () {
|
||||
Function.prototype.apply.apply(self.timeout.clearTimeout, [j$.getGlobal(), [timeoutId]]);
|
||||
},
|
||||
handleError = function(error) {
|
||||
onException(error);
|
||||
next();
|
||||
},
|
||||
next = once(function () {
|
||||
clearTimeout(timeoutId);
|
||||
self.globalErrors.popListener(handleError);
|
||||
self.run(queueableFns, iterativeIndex + 1);
|
||||
}),
|
||||
timeoutId;
|
||||
@@ -3284,6 +3342,8 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
next();
|
||||
};
|
||||
|
||||
self.globalErrors.pushListener(handleError);
|
||||
|
||||
if (queueableFn.timeout) {
|
||||
timeoutId = Function.prototype.apply.apply(self.timeout.setTimeout, [j$.getGlobal(), [function() {
|
||||
var error = new Error('Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.');
|
||||
|
||||
94
spec/core/GlobalErrorsSpec.js
Normal file
94
spec/core/GlobalErrorsSpec.js
Normal file
@@ -0,0 +1,94 @@
|
||||
describe("GlobalErrors", function() {
|
||||
it("calls the added handler on error", function() {
|
||||
var fakeGlobal = { onerror: null },
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler);
|
||||
|
||||
fakeGlobal.onerror('foo');
|
||||
|
||||
expect(handler).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
it("only calls the most recent handler", function() {
|
||||
var fakeGlobal = { onerror: null },
|
||||
handler1 = jasmine.createSpy('errorHandler1'),
|
||||
handler2 = jasmine.createSpy('errorHandler2'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler1);
|
||||
errors.pushListener(handler2);
|
||||
|
||||
fakeGlobal.onerror('foo');
|
||||
|
||||
expect(handler1).not.toHaveBeenCalled();
|
||||
expect(handler2).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
it("calls previous handlers when one is removed", function() {
|
||||
var fakeGlobal = { onerror: null },
|
||||
handler1 = jasmine.createSpy('errorHandler1'),
|
||||
handler2 = jasmine.createSpy('errorHandler2'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler1);
|
||||
errors.pushListener(handler2);
|
||||
|
||||
errors.popListener();
|
||||
|
||||
fakeGlobal.onerror('foo');
|
||||
|
||||
expect(handler1).toHaveBeenCalledWith('foo');
|
||||
expect(handler2).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uninstalls itself, putting back a previous callback", function() {
|
||||
var originalCallback = jasmine.createSpy('error'),
|
||||
fakeGlobal = { onerror: originalCallback },
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
expect(fakeGlobal.onerror).toBe(originalCallback);
|
||||
|
||||
errors.install();
|
||||
|
||||
expect(fakeGlobal.onerror).not.toBe(originalCallback);
|
||||
|
||||
errors.uninstall();
|
||||
|
||||
expect(fakeGlobal.onerror).toBe(originalCallback);
|
||||
});
|
||||
|
||||
it("works 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('uncaughtException', jasmine.any(Function));
|
||||
expect(fakeGlobal.process.listeners).toHaveBeenCalledWith('uncaughtException');
|
||||
expect(fakeGlobal.process.removeAllListeners).toHaveBeenCalledWith('uncaughtException');
|
||||
|
||||
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('uncaughtException', addedListener);
|
||||
expect(fakeGlobal.process.on).toHaveBeenCalledWith('uncaughtException', 'foo');
|
||||
});
|
||||
});
|
||||
@@ -234,6 +234,43 @@ describe("QueueRunner", function() {
|
||||
queueRunner.execute();
|
||||
expect(doneReturn).toBe(null);
|
||||
});
|
||||
|
||||
it("continues running functions when an exception is thrown in async code without timing out", function() {
|
||||
var queueableFn = { fn: function(done) { throwAsync(); }, timeout: function() { return 1; } },
|
||||
nextQueueableFn = { fn: jasmine.createSpy("nextFunction") },
|
||||
onException = jasmine.createSpy('onException'),
|
||||
globalErrors = { pushListener: jasmine.createSpy('pushListener'), popListener: jasmine.createSpy('popListener') },
|
||||
queueRunner = new jasmineUnderTest.QueueRunner({
|
||||
queueableFns: [queueableFn, nextQueueableFn],
|
||||
onException: onException,
|
||||
globalErrors: globalErrors
|
||||
}),
|
||||
throwAsync = function() {
|
||||
globalErrors.pushListener.calls.mostRecent().args[0](new Error('foo'));
|
||||
jasmine.clock().tick(2);
|
||||
};
|
||||
|
||||
nextQueueableFn.fn.and.callFake(function() {
|
||||
// should remove the same function that was added
|
||||
expect(globalErrors.popListener).toHaveBeenCalledWith(globalErrors.pushListener.calls.argsFor(0)[0]);
|
||||
});
|
||||
|
||||
queueRunner.execute();
|
||||
|
||||
function errorWithMessage(message) {
|
||||
return {
|
||||
asymmetricMatch(other) {
|
||||
return new RegExp(message).test(other.message);
|
||||
},
|
||||
toString: function() {
|
||||
return '<Error with message like "' + message + '">';
|
||||
}
|
||||
};
|
||||
}
|
||||
expect(onException).not.toHaveBeenCalledWith(errorWithMessage(/DEFAULT_TIMEOUT_INTERVAL/));
|
||||
expect(onException).toHaveBeenCalledWith(errorWithMessage(/^foo$/));
|
||||
expect(nextQueueableFn.fn).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("calls exception handlers when an exception is thrown in a fn", function() {
|
||||
|
||||
@@ -1757,4 +1757,37 @@ describe("Env integration", function() {
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("should associate errors thrown from async code with the correct runnable", function(done) {
|
||||
var env = new jasmineUnderTest.Env(),
|
||||
reporter = jasmine.createSpyObj('fakeReport', ['jasmineDone','suiteDone','specDone']);
|
||||
|
||||
reporter.jasmineDone.and.callFake(function() {
|
||||
expect(reporter.suiteDone).toHaveFailedExpecationsForRunnable('async suite', [
|
||||
/^(((Uncaught )?Error: suite( thrown)?)|(suite thrown))$/
|
||||
]);
|
||||
expect(reporter.specDone).toHaveFailedExpecationsForRunnable('suite async spec', [
|
||||
/^(((Uncaught )?Error: spec( thrown)?)|(spec thrown))$/
|
||||
]);
|
||||
done();
|
||||
});
|
||||
|
||||
env.addReporter(reporter);
|
||||
|
||||
env.describe('async suite', function() {
|
||||
env.afterAll(function(innerDone) {
|
||||
setTimeout(function() { throw new Error('suite'); }, 1);
|
||||
}, 10);
|
||||
|
||||
env.it('spec', function() {});
|
||||
});
|
||||
|
||||
env.describe('suite', function() {
|
||||
env.it('async spec', function(innerDone) {
|
||||
setTimeout(function() { throw new Error('spec'); }, 1);
|
||||
}, 10);
|
||||
});
|
||||
|
||||
env.execute();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -40,6 +40,8 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
'specDone'
|
||||
]);
|
||||
|
||||
var globalErrors = new j$.GlobalErrors();
|
||||
|
||||
this.specFilter = function() {
|
||||
return true;
|
||||
};
|
||||
@@ -187,6 +189,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
options.clearStack = options.clearStack || clearStack;
|
||||
options.timeout = {setTimeout: realSetTimeout, clearTimeout: realClearTimeout};
|
||||
options.fail = self.fail;
|
||||
options.globalErrors = globalErrors;
|
||||
|
||||
new j$.QueueRunner(options).execute();
|
||||
};
|
||||
@@ -250,9 +253,11 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
|
||||
currentlyExecutingSuites.push(topSuite);
|
||||
|
||||
globalErrors.install();
|
||||
processor.execute(function() {
|
||||
clearResourcesForRunnable(topSuite.id);
|
||||
currentlyExecutingSuites.pop();
|
||||
globalErrors.uninstall();
|
||||
|
||||
reporter.jasmineDone({
|
||||
order: order,
|
||||
|
||||
45
src/core/GlobalErrors.js
Normal file
45
src/core/GlobalErrors.js
Normal file
@@ -0,0 +1,45 @@
|
||||
getJasmineRequireObj().GlobalErrors = function(j$) {
|
||||
function GlobalErrors(global) {
|
||||
var handlers = [];
|
||||
global = global || j$.getGlobal();
|
||||
|
||||
var onerror = function onerror() {
|
||||
var handler = handlers[handlers.length - 1];
|
||||
handler.apply(null, Array.prototype.slice.call(arguments, 0));
|
||||
};
|
||||
|
||||
this.uninstall = function noop() {};
|
||||
|
||||
this.install = function install() {
|
||||
if (global.process && 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]);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
var originalHandler = global.onerror;
|
||||
global.onerror = onerror;
|
||||
|
||||
this.uninstall = function uninstall() {
|
||||
global.onerror = originalHandler;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
this.pushListener = function pushListener(listener) {
|
||||
handlers.push(listener);
|
||||
};
|
||||
|
||||
this.popListener = function popListener() {
|
||||
handlers.pop();
|
||||
};
|
||||
}
|
||||
|
||||
return GlobalErrors;
|
||||
};
|
||||
@@ -20,6 +20,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
this.userContext = attrs.userContext || {};
|
||||
this.timeout = attrs.timeout || {setTimeout: setTimeout, clearTimeout: clearTimeout};
|
||||
this.fail = attrs.fail || function() {};
|
||||
this.globalErrors = attrs.globalErrors || { pushListener: function() {}, popListener: function() {} };
|
||||
}
|
||||
|
||||
QueueRunner.prototype.execute = function() {
|
||||
@@ -56,8 +57,13 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
var clearTimeout = function () {
|
||||
Function.prototype.apply.apply(self.timeout.clearTimeout, [j$.getGlobal(), [timeoutId]]);
|
||||
},
|
||||
handleError = function(error) {
|
||||
onException(error);
|
||||
next();
|
||||
},
|
||||
next = once(function () {
|
||||
clearTimeout(timeoutId);
|
||||
self.globalErrors.popListener(handleError);
|
||||
self.run(queueableFns, iterativeIndex + 1);
|
||||
}),
|
||||
timeoutId;
|
||||
@@ -67,6 +73,8 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
||||
next();
|
||||
};
|
||||
|
||||
self.globalErrors.pushListener(handleError);
|
||||
|
||||
if (queueableFn.timeout) {
|
||||
timeoutId = Function.prototype.apply.apply(self.timeout.setTimeout, [j$.getGlobal(), [function() {
|
||||
var error = new Error('Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.');
|
||||
|
||||
@@ -57,6 +57,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
|
||||
j$.DiffBuilder = jRequire.DiffBuilder(j$);
|
||||
j$.NullDiffBuilder = jRequire.NullDiffBuilder(j$);
|
||||
j$.ObjectPath = jRequire.ObjectPath(j$);
|
||||
j$.GlobalErrors = jRequire.GlobalErrors(j$);
|
||||
|
||||
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user