Support detectLateRejectionHandling in beforeAll and afterAll

This commit is contained in:
Steve Gravrock
2025-09-02 09:58:46 -07:00
parent 60f34ec087
commit 3698f6fb5d
4 changed files with 452 additions and 187 deletions

View File

@@ -117,19 +117,7 @@ getJasmineRequireObj().TreeRunner = function(j$) {
fns.unshift(start);
if (config.detectLateRejectionHandling) {
// Conditional because the setTimeout imposes a significant performance
// penalty in suites with lots of fast specs.
const globalErrors = this.#globalErrors;
fns.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();
});
}
});
fns.push(this.#lateUnhandledRejectionChecker());
}
fns.push(complete);
@@ -138,11 +126,32 @@ getJasmineRequireObj().TreeRunner = function(j$) {
#executeSuiteSegment(suite, segmentNumber, done) {
const isTopSuite = suite === this.#executionTree.topSuite;
const isExcluded = this.#executionTree.isExcluded(suite);
let befores = [];
let afters = [];
if (suite.beforeAllFns.length > 0 && !isExcluded) {
befores = [...suite.beforeAllFns];
if (this.#getConfig().detectLateRejectionHandling) {
befores.push(this.#lateUnhandledRejectionChecker());
}
}
if (suite.afterAllFns.length > 0 && !isExcluded) {
afters = [...suite.afterAllFns];
if (this.#getConfig().detectLateRejectionHandling) {
afters.push(this.#lateUnhandledRejectionChecker());
}
}
const children = isTopSuite
? this.#executionTree.childrenOfTopSuite()
: this.#executionTree.childrenOfSuiteSegment(suite, segmentNumber);
const wrappedChildren = this.#wrapNodes(children);
const queueableFns = this.#addBeforeAndAfterAlls(suite, wrappedChildren);
const queueableFns = [
...befores,
...this.#wrapNodes(children),
...afters
];
if (!isTopSuite) {
queueableFns.unshift({
@@ -173,6 +182,25 @@ getJasmineRequireObj().TreeRunner = function(j$) {
});
}
// Returns a queueable fn that reports any still-unhandled rejections in
// cases where detectLateRejectionHandling is enabled. Should only be called
// when detectLateRejectionHandling is enabled, because the setTimeout
// imposes a significant performance penalty in suites with lots of fast
// specs.
#lateUnhandledRejectionChecker() {
const globalErrors = this.#globalErrors;
return {
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();
});
}
};
}
#suiteSegmentStart(suite, next) {
this.#currentRunableTracker.pushSuite(suite);
this.#runableResources.initForRunable(suite.id, suite.parentSuite.id);
@@ -262,16 +290,6 @@ getJasmineRequireObj().TreeRunner = function(j$) {
}
}
#addBeforeAndAfterAlls(suite, wrappedChildren) {
if (this.#executionTree.isExcluded(suite)) {
return wrappedChildren;
}
return suite.beforeAllFns
.concat(wrappedChildren)
.concat(suite.afterAllFns);
}
#suiteSkipPolicy() {
if (this.#getConfig().stopOnSpecFailure) {
return j$.CompleteOnFirstErrorSkipPolicy;