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

@@ -760,6 +760,7 @@ getJasmineRequireObj().Spec = function(j$) {
function Spec(attrs) {
this.expectationFactory = attrs.expectationFactory;
this.asyncExpectationFactory = attrs.asyncExpectationFactory;
this.setTimeout = attrs.setTimeout;
this.resultCallback = attrs.resultCallback || function() {};
this.id = attrs.id;
this.filename = attrs.filename;
@@ -830,9 +831,11 @@ getJasmineRequireObj().Spec = function(j$) {
Spec.prototype.execute = function(
queueRunnerFactory,
globalErrors,
onComplete,
excluded,
failSpecWithNoExp
failSpecWithNoExp,
detectLateRejectionHandling
) {
const onStart = {
fn: done => {
@@ -893,6 +896,21 @@ getJasmineRequireObj().Spec = function(j$) {
}
runnerConfig.queueableFns.unshift(onStart);
if (detectLateRejectionHandling) {
// Conditional because the setTimeout imposes a significant performance
// penalty in suites with lots of fast specs.
runnerConfig.queueableFns.push({
fn: done => {
// setTimeout is necessary to trigger rejectionhandled events
// TODO: let clearStack know about this so it doesn't do redundant setTimeouts
this.setTimeout(function() {
globalErrors.reportUnhandledRejections();
done();
});
}
});
}
runnerConfig.queueableFns.push(complete);
queueRunnerFactory(runnerConfig);
@@ -1190,7 +1208,12 @@ getJasmineRequireObj().Env = function(j$) {
new j$.MockDate(global)
);
const globalErrors = new j$.GlobalErrors();
const globalErrors = new j$.GlobalErrors(
undefined,
// Configuration is late-bound because GlobalErrors needs to be constructed
// before it's set to detect load-time errors in browsers
() => this.configuration()
);
const installGlobalErrors = (function() {
let installed = false;
return function() {
@@ -1324,7 +1347,21 @@ getJasmineRequireObj().Env = function(j$) {
* @type Boolean
* @default false
*/
verboseDeprecations: false
verboseDeprecations: false,
/**
* Whether to detect late promise rejection handling during spec
* execution. If this option is enabled, a promise rejection that triggers
* the JavaScript runtime's unhandled rejection event will not be treated
* as an error as long as it's handled before the spec finishes.
*
* This option is off by default because it imposes a performance penalty.
* @name Configuration#detectLateRejectionHandling
* @since 5.10.0
* @type Boolean
* @default false
*/
detectLateRejectionHandling: false
};
if (!options.suppressLoadErrors) {
@@ -1362,7 +1399,8 @@ getJasmineRequireObj().Env = function(j$) {
'stopOnSpecFailure',
'stopSpecOnExpectationFailure',
'autoCleanClosures',
'forbidDuplicateNames'
'forbidDuplicateNames',
'detectLateRejectionHandling'
];
booleanProps.forEach(function(prop) {
@@ -1664,6 +1702,7 @@ getJasmineRequireObj().Env = function(j$) {
reporter,
queueRunnerFactory,
TreeProcessor: j$.TreeProcessor,
globalErrors,
getConfig: () => config,
reportSpecDone
});
@@ -4306,28 +4345,36 @@ getJasmineRequireObj().formatErrorMsg = function() {
getJasmineRequireObj().GlobalErrors = function(j$) {
class GlobalErrors {
#getConfig;
#adapter;
#handlers;
#overrideHandler;
#onRemoveOverrideHandler;
#pendingUnhandledRejections;
constructor(global) {
constructor(global, getConfig) {
global = global || j$.getGlobal();
const dispatchError = this.#dispatchError.bind(this);
this.#getConfig = getConfig;
this.#pendingUnhandledRejections = new Map();
this.#handlers = [];
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
const dispatch = {
onUncaughtException: this.#onUncaughtException.bind(this),
onUnhandledRejection: this.#onUnhandledRejection.bind(this),
onRejectionHandled: this.#onRejectionHandled.bind(this)
};
if (
global.process &&
global.process.listeners &&
j$.isFunction_(global.process.on)
) {
this.#adapter = new NodeAdapter(global, dispatchError);
this.#adapter = new NodeAdapter(global, dispatch);
} else {
this.#adapter = new BrowserAdapter(global, dispatchError);
this.#adapter = new BrowserAdapter(global, dispatch);
}
this.#handlers = [];
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
}
install() {
@@ -4375,6 +4422,41 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#onRemoveOverrideHandler = null;
}
reportUnhandledRejections() {
for (const {
reason,
event
} of this.#pendingUnhandledRejections.values()) {
this.#dispatchError(reason, event);
}
this.#pendingUnhandledRejections.clear();
}
// Either error or event may be undefined
#onUncaughtException(error, event) {
this.#dispatchError(error, event);
}
// event or promise may be undefined
// event is passed through for backwards compatibility reasons. It's probably
// unnecessary, but user code could depend on it.
#onUnhandledRejection(reason, promise, event) {
if (this.#detectLateRejectionHandling() && promise) {
this.#pendingUnhandledRejections.set(promise, { reason, event });
} else {
this.#dispatchError(reason, event);
}
}
#detectLateRejectionHandling() {
return this.#getConfig().detectLateRejectionHandling;
}
#onRejectionHandled(promise) {
this.#pendingUnhandledRejections.delete(promise);
}
// Either error or event may be undefined
#dispatchError(error, event) {
if (this.#overrideHandler) {
@@ -4395,24 +4477,29 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
class BrowserAdapter {
#global;
#dispatchError;
#dispatch;
#onError;
#onUnhandledRejection;
#onRejectionHandled;
constructor(global, dispatchError) {
constructor(global, dispatch) {
this.#global = global;
this.#dispatchError = dispatchError;
this.#onError = event => this.#dispatchError(event.error, event);
this.#dispatch = dispatch;
this.#onError = event => dispatch.onUncaughtException(event.error, event);
this.#onUnhandledRejection = this.#unhandledRejectionHandler.bind(this);
this.#onRejectionHandled = this.#rejectionHandledHandler.bind(this);
}
install() {
this.#global.addEventListener('error', this.#onError);
this.#global.addEventListener(
'unhandledrejection',
this.#onUnhandledRejection
);
this.#global.addEventListener(
'rejectionhandled',
this.#onRejectionHandled
);
}
uninstall() {
@@ -4421,50 +4508,55 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
'unhandledrejection',
this.#onUnhandledRejection
);
this.#global.removeEventListener(
'rejectionhandled',
this.#onRejectionHandled
);
}
#unhandledRejectionHandler(event) {
const jasmineMessage = 'Unhandled promise rejection: ' + event.reason;
let reason;
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
this.#dispatchError(event.reason, event);
reason = event.reason;
reason.jasmineMessage = jasmineMessage;
} else {
this.#dispatchError(
'Unhandled promise rejection: ' + event.reason,
event
);
reason = jasmineMessage;
}
this.#dispatch.onUnhandledRejection(reason, event.promise, event);
}
#rejectionHandledHandler(event) {
this.#dispatch.onRejectionHandled(event.promise);
}
}
class NodeAdapter {
#global;
#dispatchError;
#dispatch;
#originalHandlers;
#jasmineHandlers;
#onError;
#onUnhandledRejection;
constructor(global, dispatchError) {
constructor(global, dispatch) {
this.#global = global;
this.#dispatchError = dispatchError;
this.#dispatch = dispatch;
this.#jasmineHandlers = {};
this.#originalHandlers = {};
this.#onError = error =>
this.#eventHandler(error, 'uncaughtException', 'Uncaught exception');
this.#onUnhandledRejection = error =>
this.#eventHandler(
error,
'unhandledRejection',
'Unhandled promise rejection'
);
this.onError = this.onError.bind(this);
this.onUnhandledRejection = this.onUnhandledRejection.bind(this);
}
install() {
this.#installHandler('uncaughtException', this.#onError);
this.#installHandler('unhandledRejection', this.#onUnhandledRejection);
this.#installHandler('uncaughtException', this.onError);
this.#installHandler('unhandledRejection', this.onUnhandledRejection);
this.#installHandler(
'rejectionHandled',
this.#dispatch.onRejectionHandled
);
}
uninstall() {
@@ -4496,31 +4588,49 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#global.process.on(errorType, handler);
}
#eventHandler(error, errorType, jasmineMessage) {
#augmentError(error, isUnhandledRejection) {
let jasmineMessagePrefix;
if (isUnhandledRejection) {
jasmineMessagePrefix = 'Unhandled promise rejection';
} else {
jasmineMessagePrefix = 'Uncaught exception';
}
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
error.jasmineMessage = jasmineMessagePrefix + ': ' + error;
return error;
} else {
let substituteMsg;
if (error) {
substituteMsg = jasmineMessage + ': ' + error;
substituteMsg = jasmineMessagePrefix + ': ' + error;
} else {
substituteMsg = jasmineMessage + ' with no error or message';
substituteMsg = jasmineMessagePrefix + ' with no error or message';
}
if (errorType === 'unhandledRejection') {
if (isUnhandledRejection) {
substituteMsg +=
'\n' +
'(Tip: to get a useful stack trace, use ' +
'Promise.reject(new Error(...)) instead of Promise.reject(' +
'Promise.reject(n' +
'ew Error(...)) instead of Promise.reject(' +
(error ? '...' : '') +
').)';
}
error = new Error(substituteMsg);
return new Error(substituteMsg);
}
}
this.#dispatchError(error);
onError(error) {
error = this.#augmentError(error, false);
this.#dispatch.onUncaughtException(error);
}
onUnhandledRejection(reason, promise) {
reason = this.#augmentError(reason, true);
this.#dispatch.onUnhandledRejection(reason, promise);
}
}
@@ -9286,6 +9396,7 @@ getJasmineRequireObj().Runner = function(j$) {
this.runableResources_ = options.runableResources;
this.queueRunnerFactory_ = options.queueRunnerFactory;
this.TreeProcessor_ = options.TreeProcessor;
this.globalErrors_ = options.globalErrors;
this.reporter_ = options.reporter;
this.getConfig_ = options.getConfig;
this.reportSpecDone_ = options.reportSpecDone;
@@ -9351,7 +9462,9 @@ getJasmineRequireObj().Runner = function(j$) {
return this.queueRunnerFactory_(options);
},
globalErrors: this.globalErrors_,
failSpecWithNoExpectations: config.failSpecWithNoExpectations,
detectLateRejectionHandling: config.detectLateRejectionHandling,
nodeStart: (suite, next) => {
this.currentlyExecutingSuites_.push(suite);
this.runableResources_.initForRunable(suite.id, suite.parentSuite.id);
@@ -11016,6 +11129,7 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
const config = this.env_.configuration();
const suite = this.currentDeclarationSuite_;
const parentSuiteId = suite === this.topSuite ? null : suite.id;
const global = j$.getGlobal();
const spec = new j$.Spec({
id: 'spec' + this.nextSpecId_++,
filename,
@@ -11023,6 +11137,7 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
beforeAndAfterFns: beforeAndAfterFns(suite),
expectationFactory: this.expectationFactory_,
asyncExpectationFactory: this.specAsyncExpectationFactory_,
setTimeout: global.setTimeout.bind(global),
onLateError: this.onLateError_,
resultCallback: (result, next) => {
this.specResultCallback_(spec, result, next);
@@ -11173,6 +11288,9 @@ getJasmineRequireObj().TreeProcessor = function() {
const nodeStart = attrs.nodeStart || function() {};
const nodeComplete = attrs.nodeComplete || function() {};
const failSpecWithNoExpectations = !!attrs.failSpecWithNoExpectations;
const detectLateRejectionHandling = !!attrs.detectLateRejectionHandling;
const globalErrors = attrs.globalErrors;
const orderChildren =
attrs.orderChildren ||
function(node) {
@@ -11400,9 +11518,11 @@ getJasmineRequireObj().TreeProcessor = function() {
fn: function(done) {
node.execute(
queueRunnerFactory,
globalErrors,
done,
stats[node.id].excluded,
failSpecWithNoExpectations
failSpecWithNoExpectations,
detectLateRejectionHandling
);
}
};