Skip to content

Commit 55baae1

Browse files
committed
fix(material-experimental): throw better error when trying to use fixture after it has been 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 throw a custom error that should be easier to follow.
1 parent 31d8819 commit 55baae1

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 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,6 +57,10 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
5457
}
5558

5659
private async _stabilize(): Promise<void> {
60+
if (this._destroyed) {
61+
throw Error('Harness is attempting to use a fixture that has already been destroyed.');
62+
}
63+
5764
this._fixture.detectChanges();
5865
await this._fixture.whenStable();
5966
}

0 commit comments

Comments
 (0)