Skip to content

Commit

Permalink
fix(material-experimental): throw better error when trying to use fix…
Browse files Browse the repository at this point in the history
…ture 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.
  • Loading branch information
crisbeto committed Sep 18, 2019
1 parent 14cdd89 commit 17c951d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/cdk/testing/testbed/testbed-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {UnitTestElement} from './unit-test-element';

/** A `HarnessEnvironment` implementation for Angular's Testbed. */
export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
private _destroyed = false;

protected constructor(rawRootElement: Element, private _fixture: ComponentFixture<unknown>) {
super(rawRootElement);
_fixture.componentRef.onDestroy(() => this._destroyed = true);
}

/** Creates a `HarnessLoader` rooted at the given fixture's root element. */
Expand Down Expand Up @@ -62,6 +65,10 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
}

private async _stabilize(): Promise<void> {
if (this._destroyed) {
throw Error('Harness is attempting to use a fixture that has already been destroyed.');
}

this._fixture.detectChanges();
await this._fixture.whenStable();
}
Expand Down

0 comments on commit 17c951d

Please sign in to comment.