diff --git a/src/cdk/testing/testbed/task-state-zone-interceptor.ts b/src/cdk/testing/testbed/task-state-zone-interceptor.ts index 1b91aaad34ca..91accb92016b 100644 --- a/src/cdk/testing/testbed/task-state-zone-interceptor.ts +++ b/src/cdk/testing/testbed/task-state-zone-interceptor.ts @@ -56,6 +56,13 @@ export class TaskStateZoneInterceptor { return {stable: !state.macroTask && !state.microTask}; } + static isInProxyZone(): boolean { + if (typeof Zone === 'undefined') { + return false; + } + return ((Zone as any)['ProxyZoneSpec'] as ProxyZoneStatic | undefined)?.get() !== undefined; + } + /** * Sets up the custom task state Zone interceptor in the `ProxyZone`. Throws if * no `ProxyZone` could be found. diff --git a/src/cdk/testing/testbed/testbed-harness-environment.ts b/src/cdk/testing/testbed/testbed-harness-environment.ts index 11805d6b0fe4..eab4dfc8bf5b 100644 --- a/src/cdk/testing/testbed/testbed-harness-environment.ts +++ b/src/cdk/testing/testbed/testbed-harness-environment.ts @@ -106,7 +106,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment { ) { super(rawRootElement); this._options = {...defaultEnvironmentOptions, ...options}; - if (typeof Zone !== 'undefined') { + if (TaskStateZoneInterceptor.isInProxyZone()) { this._taskState = TaskStateZoneInterceptor.setup(); } this._stabilizeCallback = () => this.forceStabilize(); @@ -178,6 +178,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment { /** * Waits for all scheduled or running async tasks to complete. This allows harness * authors to wait for async tasks outside of the Angular zone. + * + * This only works when Zone.js is present _and_ patches are applied to the test framework + * by `zone.js/testing` (Jasmine and Jest only) or another script. */ async waitForTasksOutsideAngular(): Promise { // If we run in the fake async zone, we run "flush" to run any scheduled tasks. This