Skip to content

Commit

Permalink
fix(cdk/testing): TestbedHarnessEnvironment should work when Zone is …
Browse files Browse the repository at this point in the history
…not present (#29176)

This commit fixes the harness environment which assumes Zone is always
defined on the page.
  • Loading branch information
atscott committed Jun 4, 2024
1 parent 71297ad commit f4387fd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/cdk/testing/testbed/testbed-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function uninstallAutoChangeDetectionStatusHandler(fixture: ComponentFixture<unk

/** Whether we are currently in the fake async zone. */
function isInFakeAsyncZone() {
return Zone!.current.get('FakeAsyncTestZoneSpec') != null;
return typeof Zone !== 'undefined' && Zone!.current.get('FakeAsyncTestZoneSpec') != null;
}

/**
Expand All @@ -91,7 +91,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
private _destroyed = false;

/** Observable that emits whenever the test task state changes. */
private _taskState: Observable<TaskState>;
private _taskState?: Observable<TaskState>;

/** The options for this environment. */
private _options: TestbedHarnessEnvironmentOptions;
Expand All @@ -106,7 +106,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
) {
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(() => {
Expand Down Expand Up @@ -192,7 +194,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
// 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. */
Expand Down

0 comments on commit f4387fd

Please sign in to comment.