Skip to content

Commit 66c8708

Browse files
crisbetojelbourn
authored andcommitted
fix(material-experimental): throw better error when trying to use fixture after it has been destroyed (#17058)
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 d854e6f commit 66c8708

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
protected 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. */
@@ -62,6 +65,10 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
6265
}
6366

6467
private async _stabilize(): Promise<void> {
68+
if (this._destroyed) {
69+
throw Error('Harness is attempting to use a fixture that has already been destroyed.');
70+
}
71+
6572
this._fixture.detectChanges();
6673
await this._fixture.whenStable();
6774
}

0 commit comments

Comments
 (0)