Tuesday, 30 November 2021

Harness is attempting to use a fixture that has already been destroyed

I have a trivial unit test that passes but throws the error, "Harness is attempting to use a fixture that has already been destroyed" when I run ng test --watch=false. Is this happening because I'm placing my component in <ng-template>? Does anyone know how to resolve this error?

MenuComponent Spec

describe("MenuComponent", () => {
    let testHostFixture: ComponentFixture<TestHostComponent>;
    let myMenuHarness: MenuHarness;

    beforeEach(
        waitForAsync(() => {
            TestBed.configureTestingModule({
                declarations: [...],
                imports: [...],
                providers: [...],
            }).compileComponents();
        })
    );

    beforeEach(async () => {
        testHostFixture = TestBed.createComponent(TestHostComponent);
        myMenuHarness = await TestbedHarnessEnvironment.harnessForFixture(
            testHostFixture,
            MenuHarness
        );
        testHostFixture.detectChanges();
    });

    describe("simple test", () => {
        it("should pass", async () => {
            await myMenuHarness.openMenu();
            expect(true);
        });
    });

    @Component({
        selector: `my-test-host-component`,
        template: `<kendo-grid ...>
            <ng-template kendoGridColumnMenuTemplate let-service="service">
                <app-my-menu [service]="service"></app-my-menu>
            </ng-template>
            <kendo-grid-column ... ></kendo-grid-column>
        </kendo-grid>`,
    })
    class TestHostComponent {
        @ViewChild(MenuComponent)
        public menu?: MenuComponent;
    }
});

MenuHarness

public async openColumnMenu() {
    const element = await this.queryColumnMenuButton();
    element?.click();
}

Output

Error: Harness is attempting to use a fixture that has already been destroyed.
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:720:23
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
    at _next (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:25:1)     
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:32:1
    at new ZoneAwarePromise (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:1340:1)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:21:1
    at TestbedHarnessEnvironment.forceStabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:717:28)
    at UnitTestElement._stabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:751:56)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:449:20, 'Error: Harness is attempting to use a fixture that has already been destroyed.
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:720:23
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
    at _next (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:25:1)     
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:32:1
    at new ZoneAwarePromise (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:1340:1)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:21:1
    at TestbedHarnessEnvironment.forceStabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:717:28)
    at UnitTestElement._stabilize (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:751:56)
    at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/cdk/fesm2020/testing/testbed.mjs:449:20'


from Harness is attempting to use a fixture that has already been destroyed

No comments:

Post a Comment