From f4387fda12e3e647e807111344c793efa1d14235 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 4 Jun 2024 11:06:03 -0700 Subject: [PATCH] fix(cdk/testing): TestbedHarnessEnvironment should work when Zone is not present (#29176) This commit fixes the harness environment which assumes Zone is always defined on the page. --- src/cdk/testing/testbed/testbed-harness-environment.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cdk/testing/testbed/testbed-harness-environment.ts b/src/cdk/testing/testbed/testbed-harness-environment.ts index 60f061853dc4..2db6bcc39217 100644 --- a/src/cdk/testing/testbed/testbed-harness-environment.ts +++ b/src/cdk/testing/testbed/testbed-harness-environment.ts @@ -69,7 +69,7 @@ function uninstallAutoChangeDetectionStatusHandler(fixture: ComponentFixture { private _destroyed = false; /** Observable that emits whenever the test task state changes. */ - private _taskState: Observable; + private _taskState?: Observable; /** The options for this environment. */ private _options: TestbedHarnessEnvironmentOptions; @@ -106,7 +106,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment { ) { super(rawRootElement); this._options = {...defaultEnvironmentOptions, ...options}; - this._taskState = TaskStateZoneInterceptor.setup(); + if (typeof Zone !== 'undefined') { + this._taskState = TaskStateZoneInterceptor.setup(); + } this._stabilizeCallback = () => this.forceStabilize(); installAutoChangeDetectionStatusHandler(_fixture); _fixture.componentRef.onDestroy(() => { @@ -192,7 +194,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment { // we cannot rely on "fixture.whenStable" since it does not catch tasks scheduled // outside of the Angular zone. For test harnesses, we want to ensure that the // app is fully stabilized and therefore need to use our own zone interceptor. - await this._taskState.pipe(takeWhile(state => !state.stable)).toPromise(); + await this._taskState?.pipe(takeWhile(state => !state.stable)).toPromise(); } /** Gets the root element for the document. */