Skip to content

Commit dd40f25

Browse files
authored
fix(cdk/testing): Skip task tracking if zone patches aren't present (#32544)
This commit omits the initialization of the `taskState` when the constructor executes outside the proxy zone. This would generally indicate that either `zone.js/dist/zone-testing.js` was not included _or_ it does not include patches for the test framework being used (e.g. Vitest). In this case, we should simply omit the initialization of task state tracking, meaning that `waitForTasksOutsideAngular` will "not work" and simply be a `Promise.resolve`. fixes #32542
1 parent e63c564 commit dd40f25

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/cdk/testing/testbed/task-state-zone-interceptor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export class TaskStateZoneInterceptor {
5656
return {stable: !state.macroTask && !state.microTask};
5757
}
5858

59+
static isInProxyZone(): boolean {
60+
if (typeof Zone === 'undefined') {
61+
return false;
62+
}
63+
return ((Zone as any)['ProxyZoneSpec'] as ProxyZoneStatic | undefined)?.get() !== undefined;
64+
}
65+
5966
/**
6067
* Sets up the custom task state Zone interceptor in the `ProxyZone`. Throws if
6168
* no `ProxyZone` could be found.

src/cdk/testing/testbed/testbed-harness-environment.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
106106
) {
107107
super(rawRootElement);
108108
this._options = {...defaultEnvironmentOptions, ...options};
109-
if (typeof Zone !== 'undefined') {
109+
if (TaskStateZoneInterceptor.isInProxyZone()) {
110110
this._taskState = TaskStateZoneInterceptor.setup();
111111
}
112112
this._stabilizeCallback = () => this.forceStabilize();
@@ -178,6 +178,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
178178
/**
179179
* Waits for all scheduled or running async tasks to complete. This allows harness
180180
* authors to wait for async tasks outside of the Angular zone.
181+
*
182+
* This only works when Zone.js is present _and_ patches are applied to the test framework
183+
* by `zone.js/testing` (Jasmine and Jest only) or another script.
181184
*/
182185
async waitForTasksOutsideAngular(): Promise<void> {
183186
// If we run in the fake async zone, we run "flush" to run any scheduled tasks. This

0 commit comments

Comments
 (0)