Skip to content

Commit c04a1d0

Browse files
committed
fix(material-experimental): avoid error when trying to use harness after fixture is destroyed
Currently before most operations in the harnesses we call `fixture.detectChange`, however doing so on a destroyed fixture throws a vague error. These changes avoid detecting changes on destroyed fixtures.
1 parent 31d8819 commit c04a1d0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import {UnitTestElement} from './unit-test-element';
1414

1515
/** A `HarnessEnvironment` implementation for Angular's Testbed. */
1616
export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
17+
private _destroyed = false;
18+
1719
constructor(rawRootElement: Element, private _fixture: ComponentFixture<unknown>) {
1820
super(rawRootElement);
21+
_fixture.componentRef.onDestroy(() => this._destroyed = true);
1922
}
2023

2124
/** Creates a `HarnessLoader` rooted at the given fixture's root element. */
@@ -54,7 +57,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
5457
}
5558

5659
private async _stabilize(): Promise<void> {
57-
this._fixture.detectChanges();
58-
await this._fixture.whenStable();
60+
if (!this._destroyed) {
61+
this._fixture.detectChanges();
62+
await this._fixture.whenStable();
63+
}
5964
}
6065
}

0 commit comments

Comments
 (0)