Fixed global error handling when the env is executed repeatedly

This commit is contained in:
Steve Gravrock
2025-09-06 10:32:56 -07:00
parent 62b5698a99
commit 3493519c9f
3 changed files with 67 additions and 16 deletions

View File

@@ -1157,12 +1157,21 @@ getJasmineRequireObj().Env = function(j$) {
// before it's set to detect load-time errors in browsers
() => this.configuration()
);
const installGlobalErrors = (function() {
const { installGlobalErrors, uninstallGlobalErrors } = (function() {
let installed = false;
return function() {
if (!installed) {
globalErrors.install();
installed = true;
return {
installGlobalErrors() {
if (!installed) {
globalErrors.install();
installed = true;
}
},
uninstallGlobalErrors() {
if (installed) {
globalErrors.uninstall();
installed = false;
}
}
};
})();
@@ -2086,9 +2095,7 @@ getJasmineRequireObj().Env = function(j$) {
};
this.cleanup_ = function() {
if (globalErrors) {
globalErrors.uninstall();
}
uninstallGlobalErrors();
};
}

View File

@@ -1011,6 +1011,43 @@ describe('Global error handling (integration)', function() {
});
});
it('works when the suite is run multiple times', 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({ autoCleanClosures: false });
const reporter = jasmine.createSpyObj('fakeReporter', ['specDone']);
env.addReporter(reporter);
env.it('fails', function(specDone) {
setTimeout(function() {
dispatchErrorEvent(global, 'error', { error: 'fail' });
specDone();
});
});
await env.execute();
reporter.specDone.calls.reset();
await env.execute();
expect(reporter.specDone).toHaveFailedExpectationsForRunnable('fails', [
'fail thrown'
]);
});
describe('#spyOnGlobalErrorsAsync', function() {
const leftInstalledMessage =
'Global error spy was not uninstalled. ' +

View File

@@ -31,12 +31,21 @@ getJasmineRequireObj().Env = function(j$) {
// before it's set to detect load-time errors in browsers
() => this.configuration()
);
const installGlobalErrors = (function() {
const { installGlobalErrors, uninstallGlobalErrors } = (function() {
let installed = false;
return function() {
if (!installed) {
globalErrors.install();
installed = true;
return {
installGlobalErrors() {
if (!installed) {
globalErrors.install();
installed = true;
}
},
uninstallGlobalErrors() {
if (installed) {
globalErrors.uninstall();
installed = false;
}
}
};
})();
@@ -960,9 +969,7 @@ getJasmineRequireObj().Env = function(j$) {
};
this.cleanup_ = function() {
if (globalErrors) {
globalErrors.uninstall();
}
uninstallGlobalErrors();
};
}