File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ export function withEventReplay(): Provider[] {
145145

146146
appsWithEventReplay.add(appRef);
147147

148+
const appId = injector.get(APP_ID);
148149
appRef.onDestroy(() => {
149150
appsWithEventReplay.delete(appRef);
150151
// Ensure that we're always safe calling this in the browser.
151152
if (typeof ngServerMode !== 'undefined' && !ngServerMode) {
152-
const appId = injector.get(APP_ID);
153153
// `_ejsa` should be deleted when the app is destroyed, ensuring that
154154
// no elements are still captured in the global list and are not prevented
155155
// from being garbage collected.
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,16 @@ describe('event replay', () => {
207207
appRef.tick();
208208
const appId = appRef.injector.get(APP_ID);
209209

210+
// Important: This is done intentionally because `ApplicationRef` registers
211+
// `onDestroy` callbacks, and we want to ensure that they execute successfully
212+
// without resulting in any errors. This is necessary because the bodies of
213+
// these `onDestroy` callbacks use the `ngServerMode` variable.
214+
// Prior to setting this flag, the unit test was throwing a "destroyed injector"
215+
// error — but we weren't capturing it because we hadn't explicitly set the flag to false.
216+
globalThis['ngServerMode'] = false;
210217
appRef.destroy();
218+
globalThis['ngServerMode'] = undefined;
219+
211220
// This ensure that `_ejsas` for the current application is cleaned up
212221
// once the application is destroyed.
213222
expect(window._ejsas![appId]).toBeUndefined();

0 commit comments

Comments
 (0)