Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/testing): TestbedHarnessEnvironment should work when Zone is … #29176

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading