Optionally detect late promise rejections and don't report them as errors

This commit is contained in:
Steve Gravrock
2025-08-09 08:35:08 -07:00
parent 5e88fde655
commit 395ef85954
12 changed files with 991 additions and 211 deletions

View File

@@ -2,7 +2,10 @@ describe('GlobalErrors', function() {
it('calls the added handler on error', function() {
const globals = browserGlobals();
const handler = jasmine.createSpy('errorHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler);
@@ -19,7 +22,10 @@ describe('GlobalErrors', function() {
it('is not affected by overriding global.onerror', function() {
const globals = browserGlobals();
const handler = jasmine.createSpy('errorHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler);
@@ -39,7 +45,10 @@ describe('GlobalErrors', function() {
const globals = browserGlobals();
const handler1 = jasmine.createSpy('errorHandler1');
const handler2 = jasmine.createSpy('errorHandler2');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler1);
@@ -59,7 +68,10 @@ describe('GlobalErrors', function() {
const globals = browserGlobals();
const handler1 = jasmine.createSpy('errorHandler1');
const handler2 = jasmine.createSpy('errorHandler2');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler1);
@@ -86,7 +98,10 @@ describe('GlobalErrors', function() {
it('uninstalls itself', function() {
const globals = browserGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
function unrelatedListener() {}
errors.install();
@@ -98,7 +113,10 @@ describe('GlobalErrors', function() {
it('rethrows the original error when there is no handler', function() {
const globals = browserGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const originalError = new Error('nope');
errors.install();
@@ -114,7 +132,10 @@ describe('GlobalErrors', function() {
it('reports uncaught exceptions in node.js', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
function originalHandler() {}
globals.listeners.uncaughtException = [originalHandler];
@@ -144,7 +165,10 @@ describe('GlobalErrors', function() {
describe('Reporting unhandled promise rejections in node.js', function() {
it('reports rejections with `Error` reasons', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
function originalHandler() {}
globals.listeners.unhandledRejection = [originalHandler];
@@ -173,7 +197,10 @@ describe('GlobalErrors', function() {
it('reports rejections with non-`Error` reasons', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
errors.install();
@@ -193,7 +220,10 @@ describe('GlobalErrors', function() {
it('reports rejections with no reason provided', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
errors.install();
@@ -210,12 +240,149 @@ describe('GlobalErrors', function() {
undefined
);
});
describe('When detectLateRejectionHandling is true', function() {
let globals, errors;
beforeEach(function() {
globals = nodeGlobals();
errors = new jasmineUnderTest.GlobalErrors(globals.global, () => ({
detectLateRejectionHandling: true
}));
});
it('subscribes and unsubscribes from the rejectionHandled event', function() {
function originalHandler() {}
globals.global.process.on('rejectionHandled', originalHandler);
errors.install();
expect(globals.listeners.rejectionHandled).toEqual([
jasmine.any(Function)
]);
expect(globals.listeners.rejectionHandled).not.toEqual([
originalHandler
]);
errors.uninstall();
expect(globals.listeners.rejectionHandled).toEqual([originalHandler]);
});
describe("When the unhandledRejection event doesn't have a promise", function() {
it('immediately reports the rejection', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
dispatchEvent(
globals.listeners,
'unhandledRejection',
new Error('nope'),
undefined
);
expect(handler).toHaveBeenCalledWith(new Error('nope'), undefined);
expect(handler.calls.argsFor(0)[0].jasmineMessage).toBe(
'Unhandled promise rejection: Error: nope'
);
});
});
describe('When the unhandledRejection event has a promise property', function() {
it('does not immediately report the rejection', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(
globals.listeners,
'unhandledRejection',
'nope',
promise
);
expect(handler).not.toHaveBeenCalled();
});
describe('When reportUnhandledRejections is called', function() {
it('reports rejections that have not been handled', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const reason = new Error('nope');
const promise = Promise.reject(reason);
promise.catch(() => {});
dispatchEvent(
globals.listeners,
'unhandledRejection',
reason,
promise
);
errors.reportUnhandledRejections();
expect(handler).toHaveBeenCalledWith(new Error('nope'), undefined);
expect(handler.calls.argsFor(0)[0].jasmineMessage).toBe(
'Unhandled promise rejection: Error: nope'
);
});
it('does not report rejections that have been handled', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const reason = new Error('nope');
const promise = Promise.reject(reason);
promise.catch(() => {});
dispatchEvent(
globals.listeners,
'unhandledRejection',
reason,
promise
);
dispatchEvent(globals.listeners, 'rejectionHandled', promise);
errors.reportUnhandledRejections();
expect(handler).not.toHaveBeenCalled();
});
it('does not report the same rejection on subsequent calls', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(
globals.listeners,
'unhandledRejection',
'nope',
promise
);
errors.reportUnhandledRejections();
expect(handler).toHaveBeenCalled();
handler.calls.reset();
errors.reportUnhandledRejections();
expect(handler).not.toHaveBeenCalled();
});
});
});
});
});
describe('Reporting unhandled promise rejections in the browser', function() {
it('subscribes and unsubscribes from the unhandledrejection event', function() {
const globals = browserGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
expect(globals.listeners.unhandledrejection).toEqual([
@@ -229,7 +396,10 @@ describe('GlobalErrors', function() {
it('reports rejections whose reason is a string', function() {
const globals = browserGlobals();
const handler = jasmine.createSpy('errorHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler);
@@ -246,7 +416,10 @@ describe('GlobalErrors', function() {
it('reports rejections whose reason is an Error', function() {
const globals = browserGlobals();
const handler = jasmine.createSpy('errorHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler);
@@ -264,12 +437,129 @@ describe('GlobalErrors', function() {
event
);
});
describe('When detectLateRejectionHandling is true', function() {
let globals, errors;
beforeEach(function() {
globals = browserGlobals();
errors = new jasmineUnderTest.GlobalErrors(globals.global, () => ({
detectLateRejectionHandling: true
}));
});
it('subscribes and unsubscribes from the rejectionhandled event', function() {
errors.install();
expect(globals.listeners.rejectionhandled).toEqual([
jasmine.any(Function)
]);
errors.uninstall();
expect(globals.listeners.rejectionhandled).toEqual([]);
});
describe("When the unhandledrejection event doesn't have a promise property", function() {
it('immediately reports the rejection', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const event = { reason: 'nope' };
dispatchEvent(globals.listeners, 'unhandledrejection', event);
expect(handler).toHaveBeenCalledWith(
'Unhandled promise rejection: nope',
event
);
});
});
describe('When the unhandledrejection event has a promise property', function() {
it('does not immediately report the rejection', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(globals.listeners, 'unhandledrejection', {
reason: 'nope',
promise
});
expect(handler).not.toHaveBeenCalled();
});
describe('When reportUnhandledRejections is called', function() {
it('reports rejections that have not been handled', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(globals.listeners, 'unhandledrejection', {
reason: 'nope',
promise
});
errors.reportUnhandledRejections();
expect(handler).toHaveBeenCalledWith(
'Unhandled promise rejection: nope',
{ reason: 'nope', promise }
);
});
it('does not report rejections that have been handled', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(globals.listeners, 'unhandledrejection', {
reason: 'nope',
promise
});
dispatchEvent(globals.listeners, 'rejectionhandled', { promise });
errors.reportUnhandledRejections();
expect(handler).not.toHaveBeenCalled();
});
it('does not report the same rejection on subsequent calls', function() {
const handler = jasmine.createSpy('errorHandler');
errors.install();
errors.pushListener(handler);
const promise = Promise.reject('nope');
promise.catch(() => {});
dispatchEvent(globals.listeners, 'unhandledrejection', {
reason: 'nope',
promise
});
errors.reportUnhandledRejections();
expect(handler).toHaveBeenCalled();
handler.calls.reset();
errors.reportUnhandledRejections();
expect(handler).not.toHaveBeenCalled();
});
});
});
});
});
describe('Reporting uncaught exceptions in node.js', function() {
it('prepends a descriptive message when the error is not an `Error`', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
errors.install();
@@ -285,7 +575,10 @@ describe('GlobalErrors', function() {
it('substitutes a descriptive message when the error is falsy', function() {
const globals = nodeGlobals();
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
const handler = jasmine.createSpy('errorHandler');
errors.install();
@@ -306,7 +599,10 @@ describe('GlobalErrors', function() {
const handler0 = jasmine.createSpy('handler0');
const handler1 = jasmine.createSpy('handler1');
const overrideHandler = jasmine.createSpy('overrideHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler0);
@@ -331,7 +627,10 @@ describe('GlobalErrors', function() {
const handler0 = jasmine.createSpy('handler0');
const handler1 = jasmine.createSpy('handler1');
const overrideHandler = jasmine.createSpy('overrideHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler0);
@@ -356,7 +655,10 @@ describe('GlobalErrors', function() {
const globals = browserGlobals();
const handler = jasmine.createSpy('handler');
const overrideHandler = jasmine.createSpy('overrideHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler);
@@ -381,7 +683,10 @@ describe('GlobalErrors', function() {
const handler0 = jasmine.createSpy('handler0');
const handler1 = jasmine.createSpy('handler1');
const overrideHandler = jasmine.createSpy('overrideHandler');
const errors = new jasmineUnderTest.GlobalErrors(globals.global);
const errors = new jasmineUnderTest.GlobalErrors(
globals.global,
() => ({})
);
errors.install();
errors.pushListener(handler0);
@@ -424,7 +729,11 @@ describe('GlobalErrors', function() {
});
function browserGlobals() {
const listeners = { error: [], unhandledrejection: [] };
const listeners = {
error: [],
unhandledrejection: [],
rejectionhandled: []
};
return {
listeners,
global: {
@@ -441,7 +750,11 @@ describe('GlobalErrors', function() {
}
function nodeGlobals() {
const listeners = { uncaughtException: [], unhandledRejection: [] };
const listeners = {
uncaughtException: [],
unhandledRejection: [],
rejectionHandled: []
};
return {
listeners,
global: {
@@ -465,13 +778,13 @@ describe('GlobalErrors', function() {
};
}
function dispatchEvent(listeners, eventName, event) {
function dispatchEvent(listeners, eventName, ...args) {
expect(listeners[eventName].length)
.withContext(`number of ${eventName} listeners`)
.toBeGreaterThan(0);
for (const l of listeners[eventName]) {
l(event);
l.apply(null, args);
}
}
});

View File

@@ -121,6 +121,58 @@ describe('Spec', function() {
]);
});
describe('Late promise rejection handling', function() {
it('is enabled when the detectLateRejectionHandling param is true', function() {
const fakeQueueRunner = jasmine.createSpy('fakeQueueRunner');
const globalErrors = jasmine.createSpyObj('globalErrors', [
'reportUnhandledRejections'
]);
const setTimeout = jasmine.createSpy('setTimeout');
const before = jasmine.createSpy('before');
const after = jasmine.createSpy('after');
const queueableFn = {
fn: jasmine.createSpy('test body').and.callFake(function() {
expect(before).toHaveBeenCalled();
expect(after).not.toHaveBeenCalled();
})
};
const spec = new jasmineUnderTest.Spec({
queueableFn,
setTimeout,
beforeAndAfterFns: function() {
return { befores: [before], afters: [after] };
}
});
spec.execute(fakeQueueRunner, globalErrors, null, false, false, true);
const options = fakeQueueRunner.calls.mostRecent().args[0];
expect(options.queueableFns).toEqual([
{ fn: jasmine.any(Function) },
before,
queueableFn,
after,
{ fn: jasmine.any(Function) },
{
fn: jasmine.any(Function),
type: 'specCleanup'
}
]);
const done = jasmine.createSpy('done');
options.queueableFns[4].fn(done);
expect(globalErrors.reportUnhandledRejections).not.toHaveBeenCalled();
expect(done).not.toHaveBeenCalled();
expect(setTimeout).toHaveBeenCalledOnceWith(jasmine.any(Function));
setTimeout.calls.argsFor(0)[0]();
expect(globalErrors.reportUnhandledRejections).toHaveBeenCalled();
expect(globalErrors.reportUnhandledRejections).toHaveBeenCalledBefore(
done
);
});
});
it("tells the queue runner that it's a leaf node", function() {
const fakeQueueRunner = jasmine.createSpy('fakeQueueRunner'),
spec = new jasmineUnderTest.Spec({
@@ -162,7 +214,7 @@ describe('Spec', function() {
resultCallback: resultCallback
});
spec.execute(fakeQueueRunner, 'cally-back', true);
spec.execute(fakeQueueRunner, null, 'cally-back', true);
expect(fakeQueueRunner).toHaveBeenCalledWith(
jasmine.objectContaining({
@@ -245,7 +297,7 @@ describe('Spec', function() {
resultCallback: function() {}
});
spec.execute(attrs => attrs.onComplete(), done);
spec.execute(attrs => attrs.onComplete(), null, done);
expect(done).toHaveBeenCalled();
});
@@ -264,7 +316,7 @@ describe('Spec', function() {
spec.result.status = 'failed';
attrs.onComplete();
}
spec.execute(queueRunnerFactory, done);
spec.execute(queueRunnerFactory, null, done);
expect(done).toHaveBeenCalledWith(
jasmine.any(jasmineUnderTest.StopExecutionError)
@@ -295,7 +347,7 @@ describe('Spec', function() {
config.onComplete();
}
spec.execute(queueRunnerFactory, function() {});
spec.execute(queueRunnerFactory, null, function() {});
expect(duration).toBe(77000);
});
@@ -309,7 +361,7 @@ describe('Spec', function() {
resultCallback: function() {}
});
spec.setSpecProperty('a', 4);
spec.execute(attrs => attrs.onComplete(), done);
spec.execute(attrs => attrs.onComplete(), null, done);
expect(spec.result.properties).toEqual({ a: 4 });
});
@@ -665,7 +717,7 @@ describe('Spec', function() {
config.onComplete(false);
}
spec.execute(queueRunnerFactory, function() {});
spec.execute(queueRunnerFactory, null, function() {});
expect(resultCallback).toHaveBeenCalledWith(
jasmine.objectContaining({ debugLogs: null }),
undefined
@@ -689,7 +741,7 @@ describe('Spec', function() {
config.onComplete(false);
}
spec.execute(queueRunnerFactory, function() {});
spec.execute(queueRunnerFactory, null, function() {});
expect(resultCallback).toHaveBeenCalled();
expect(spec.result.debugLogs).toBeNull();
});
@@ -719,7 +771,7 @@ describe('Spec', function() {
config.onComplete(true);
}
spec.execute(queueRunnerFactory, function() {});
spec.execute(queueRunnerFactory, null, function() {});
expect(resultCallback).toHaveBeenCalledWith(
jasmine.objectContaining({
debugLogs: [{ message: 'msg', timestamp: timestamp }]

View File

@@ -275,14 +275,21 @@ describe('TreeProcessor', function() {
});
it('runs a single leaf', async function() {
const leaf = new Leaf(),
node = new Node({ children: [leaf], userContext: { root: 'context' } }),
queueRunner = jasmine.createSpy('queueRunner'),
processor = new jasmineUnderTest.TreeProcessor({
tree: node,
runnableIds: [leaf.id],
queueRunnerFactory: queueRunner
});
const leaf = new Leaf();
const node = new Node({
children: [leaf],
userContext: { root: 'context' }
});
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = true;
const processor = new jasmineUnderTest.TreeProcessor({
tree: node,
runnableIds: [leaf.id],
queueRunnerFactory: queueRunner,
globalErrors,
detectLateRejectionHandling
});
const promise = processor.execute();
@@ -296,7 +303,14 @@ describe('TreeProcessor', function() {
const queueRunnerArgs = queueRunner.calls.mostRecent().args[0];
queueRunnerArgs.queueableFns[0].fn('foo');
expect(leaf.execute).toHaveBeenCalledWith(queueRunner, 'foo', false, false);
expect(leaf.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'foo',
false,
false,
detectLateRejectionHandling
);
queueRunnerArgs.onComplete();
await expectAsync(promise).toBeResolvedTo(undefined);
@@ -355,18 +369,22 @@ describe('TreeProcessor', function() {
});
it('runs a node with children', function() {
const leaf1 = new Leaf(),
leaf2 = new Leaf(),
node = new Node({ children: [leaf1, leaf2] }),
root = new Node({ children: [node] }),
queueRunner = jasmine.createSpy('queueRunner'),
processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [node.id],
queueRunnerFactory: queueRunner
}),
treeComplete = jasmine.createSpy('treeComplete'),
nodeDone = jasmine.createSpy('nodeDone');
const leaf1 = new Leaf();
const leaf2 = new Leaf();
const node = new Node({ children: [leaf1, leaf2] });
const root = new Node({ children: [node] });
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = false;
const processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [node.id],
queueRunnerFactory: queueRunner,
globalErrors,
detectLateRejectionHandling
});
const treeComplete = jasmine.createSpy('treeComplete');
const nodeDone = jasmine.createSpy('nodeDone');
processor.execute(treeComplete);
let queueableFns = queueRunner.calls.mostRecent().args[0].queueableFns;
@@ -378,34 +396,42 @@ describe('TreeProcessor', function() {
queueableFns[1].fn('foo');
expect(leaf1.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'foo',
false,
false
false,
detectLateRejectionHandling
);
queueableFns[2].fn('bar');
expect(leaf2.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'bar',
false,
false
false,
detectLateRejectionHandling
);
});
it('cascades errors up the tree', function() {
const leaf = new Leaf(),
node = new Node({ children: [leaf] }),
root = new Node({ children: [node] }),
queueRunner = jasmine.createSpy('queueRunner'),
nodeComplete = jasmine.createSpy('nodeComplete'),
processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [node.id],
nodeComplete: nodeComplete,
queueRunnerFactory: queueRunner
}),
treeComplete = jasmine.createSpy('treeComplete'),
nodeDone = jasmine.createSpy('nodeDone');
const leaf = new Leaf();
const node = new Node({ children: [leaf] });
const root = new Node({ children: [node] });
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = false;
const nodeComplete = jasmine.createSpy('nodeComplete');
const processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [node.id],
nodeComplete: nodeComplete,
queueRunnerFactory: queueRunner,
globalErrors,
detectLateRejectionHandling
});
const treeComplete = jasmine.createSpy('treeComplete');
const nodeDone = jasmine.createSpy('nodeDone');
processor.execute(treeComplete);
let queueableFns = queueRunner.calls.mostRecent().args[0].queueableFns;
@@ -415,7 +441,14 @@ describe('TreeProcessor', function() {
expect(queueableFns.length).toBe(2);
queueableFns[1].fn('foo');
expect(leaf.execute).toHaveBeenCalledWith(queueRunner, 'foo', false, false);
expect(leaf.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'foo',
false,
false,
detectLateRejectionHandling
);
queueRunner.calls.mostRecent().args[0].onComplete('things');
expect(nodeComplete).toHaveBeenCalled();
@@ -424,21 +457,25 @@ describe('TreeProcessor', function() {
});
it('runs an excluded node with leaf', function() {
const leaf1 = new Leaf(),
node = new Node({ children: [leaf1] }),
root = new Node({ children: [node] }),
queueRunner = jasmine.createSpy('queueRunner'),
nodeStart = jasmine.createSpy('nodeStart'),
nodeComplete = jasmine.createSpy('nodeComplete'),
processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [],
queueRunnerFactory: queueRunner,
nodeStart: nodeStart,
nodeComplete: nodeComplete
}),
treeComplete = jasmine.createSpy('treeComplete'),
nodeDone = jasmine.createSpy('nodeDone');
const leaf1 = new Leaf();
const node = new Node({ children: [leaf1] });
const root = new Node({ children: [node] });
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = false;
const nodeStart = jasmine.createSpy('nodeStart');
const nodeComplete = jasmine.createSpy('nodeComplete');
const processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [],
queueRunnerFactory: queueRunner,
nodeStart: nodeStart,
nodeComplete: nodeComplete,
globalErrors,
detectLateRejectionHandling
});
const treeComplete = jasmine.createSpy('treeComplete');
const nodeDone = jasmine.createSpy('nodeDone');
processor.execute(treeComplete);
let queueableFns = queueRunner.calls.mostRecent().args[0].queueableFns;
@@ -451,7 +488,14 @@ describe('TreeProcessor', function() {
expect(nodeStart).toHaveBeenCalledWith(node, 'bar');
queueableFns[1].fn('foo');
expect(leaf1.execute).toHaveBeenCalledWith(queueRunner, 'foo', true, false);
expect(leaf1.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'foo',
true,
false,
detectLateRejectionHandling
);
node.getResult.and.returnValue({ im: 'disabled' });
@@ -464,22 +508,26 @@ describe('TreeProcessor', function() {
});
it('should execute node with correct arguments when failSpecWithNoExpectations option is set', function() {
const leaf = new Leaf(),
node = new Node({ children: [leaf] }),
root = new Node({ children: [node] }),
queueRunner = jasmine.createSpy('queueRunner'),
nodeStart = jasmine.createSpy('nodeStart'),
nodeComplete = jasmine.createSpy('nodeComplete'),
processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [],
queueRunnerFactory: queueRunner,
nodeStart: nodeStart,
nodeComplete: nodeComplete,
failSpecWithNoExpectations: true
}),
treeComplete = jasmine.createSpy('treeComplete'),
nodeDone = jasmine.createSpy('nodeDone');
const leaf = new Leaf();
const node = new Node({ children: [leaf] });
const root = new Node({ children: [node] });
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = false;
const nodeStart = jasmine.createSpy('nodeStart');
const nodeComplete = jasmine.createSpy('nodeComplete');
const processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [],
queueRunnerFactory: queueRunner,
nodeStart: nodeStart,
nodeComplete: nodeComplete,
globalErrors,
detectLateRejectionHandling,
failSpecWithNoExpectations: true
});
const treeComplete = jasmine.createSpy('treeComplete');
const nodeDone = jasmine.createSpy('nodeDone');
processor.execute(treeComplete);
let queueableFns = queueRunner.calls.mostRecent().args[0].queueableFns;
@@ -489,7 +537,14 @@ describe('TreeProcessor', function() {
expect(queueableFns.length).toBe(2);
queueableFns[1].fn('foo');
expect(leaf.execute).toHaveBeenCalledWith(queueRunner, 'foo', true, true);
expect(leaf.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
'foo',
true,
true,
detectLateRejectionHandling
);
});
it('runs beforeAlls for a node with children', function() {
@@ -635,16 +690,20 @@ describe('TreeProcessor', function() {
});
it('runs specified leaves before non-specified leaves within a parent node', function() {
const specified = new Leaf(),
nonSpecified = new Leaf(),
root = new Node({ children: [nonSpecified, specified] }),
queueRunner = jasmine.createSpy('queueRunner'),
processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [specified.id],
queueRunnerFactory: queueRunner
}),
treeComplete = jasmine.createSpy('treeComplete');
const specified = new Leaf();
const nonSpecified = new Leaf();
const root = new Node({ children: [nonSpecified, specified] });
const queueRunner = jasmine.createSpy('queueRunner');
const globalErrors = 'the globalErrors instance';
const detectLateRejectionHandling = false;
const processor = new jasmineUnderTest.TreeProcessor({
tree: root,
runnableIds: [specified.id],
queueRunnerFactory: queueRunner,
globalErrors,
detectLateRejectionHandling
});
const treeComplete = jasmine.createSpy('treeComplete');
processor.execute(treeComplete);
const queueableFns = queueRunner.calls.mostRecent().args[0].queueableFns;
@@ -653,18 +712,22 @@ describe('TreeProcessor', function() {
expect(nonSpecified.execute).not.toHaveBeenCalled();
expect(specified.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
undefined,
false,
false
false,
detectLateRejectionHandling
);
queueableFns[1].fn();
expect(nonSpecified.execute).toHaveBeenCalledWith(
queueRunner,
globalErrors,
undefined,
true,
false
false,
detectLateRejectionHandling
);
});

View File

@@ -3697,7 +3697,7 @@ describe('Env integration', function() {
function browserEventMethods() {
return {
listeners_: { error: [], unhandledrejection: [] },
listeners_: { error: [], unhandledrejection: [], rejectionhandled: [] },
addEventListener(eventName, listener) {
this.listeners_[eventName].push(listener);
},

View File

@@ -802,6 +802,118 @@ describe('Global error handling (integration)', function() {
);
}
});
describe('When the detectLateRejectionHandling config option is set', function() {
describe('When the unhandled rejection event has a promise', function() {
it('reports the rejection unless a corresponding rejection handled event occurs', async function() {
function makeEvent(suffix) {
const reason = `rejection ${suffix}`;
const promise = Promise.reject(reason);
promise.catch(() => {});
return { reason, promise };
}
const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) {
return setTimeout(fn, delay);
},
clearTimeout: function(fn, delay) {
clearTimeout(fn, delay);
},
queueMicrotask: function(fn) {
queueMicrotask(fn);
}
};
spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global);
env.cleanup_();
env = new jasmineUnderTest.Env();
env.configure({ detectLateRejectionHandling: true });
const reporter = jasmine.createSpyObj('fakeReporter', [
'specDone',
'suiteDone'
]);
env.addReporter(reporter);
env.describe('A suite', function() {
env.it('fails', function(specDone) {
setTimeout(function() {
const events = ['spec 1', 'spec 2'].map(makeEvent);
for (const e of events) {
dispatchErrorEvent(global, 'unhandledrejection', e);
}
dispatchErrorEvent(global, 'rejectionhandled', events[0]);
specDone();
});
});
});
await env.execute();
expect(reporter.specDone).toHaveBeenCalledWith(
jasmine.objectContaining({
fullName: 'A suite fails',
failedExpectations: [
// Only the second rejection should be reported, since the first
// one was eventually handled.
jasmine.objectContaining({
message:
'Unhandled promise rejection: rejection spec 2 thrown'
})
]
})
);
});
});
describe("When the unhandled rejection event doesn't have a promise", function() {
it('reports the rejection', async function() {
const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) {
return setTimeout(fn, delay);
},
clearTimeout: function(fn, delay) {
clearTimeout(fn, delay);
},
queueMicrotask: function(fn) {
queueMicrotask(fn);
}
};
spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global);
env.cleanup_();
env = new jasmineUnderTest.Env();
env.configure({ detectLateRejectionHandling: true });
const reporter = jasmine.createSpyObj('fakeReporter', [
'specDone',
'suiteDone'
]);
env.addReporter(reporter);
env.describe('A suite', function() {
env.it('fails', function(specDone) {
setTimeout(function() {
dispatchErrorEvent(global, 'unhandledrejection', {
reason: 'fail'
});
specDone();
});
});
});
await env.execute();
expect(reporter.specDone).toHaveFailedExpectationsForRunnable(
'A suite fails',
['Unhandled promise rejection: fail thrown']
);
});
});
});
});
describe('#spyOnGlobalErrorsAsync', function() {
@@ -1147,7 +1259,7 @@ describe('Global error handling (integration)', function() {
function browserEventMethods() {
return {
listeners_: { error: [], unhandledrejection: [] },
listeners_: { error: [], unhandledrejection: [], rejectionhandled: [] },
addEventListener(eventName, listener) {
this.listeners_[eventName].push(listener);
},