testing-library/angular-testing-library

NG0911: View has already been destroyed

marcus-sa opened this issue · 5 comments

This test breaks after upgrading Angular dependencies from 16.2.8 to 17.0.3 and @testing-library/angular from 14.3.0 to 14.5.0
https://github.com/marcus-sa/ngkit/blob/9d6ee2c875cdede09671a7a0673b378453801a77/packages/core/src/lib/integration.spec.ts#L35-L46

stderr | src/lib/integration.spec.ts > declaration specific dependencies should be available for components
RuntimeError: NG0911: View has already been destroyed.
    at storeLViewOnDestroy (file:///Users/marcus-sa/Git/packages/core/src/render3/util/view_utils.ts:254:4)
    at ViewRef$1.onDestroy (file:///Users/marcus-sa/Git/deepkit/ngkit/node_modules/@angular/core/fesm2022/core.mjs:13735:9)
    at ComponentRef.onDestroy (file:///Users/marcus-sa/Git/packages/core/src/render3/component_ref.ts:376:10)
    at renderFixture (file:///Users/marcus-sa/Git/deepkit/ngkit/projects/testing-library/src/lib/testing-library.ts:260:33)
    at Module.render (file:///Users/marcus-sa/Git/deepkit/ngkit/projects/testing-library/src/lib/testing-library.ts:166:19) {
  code: 911
}

Are you sure this is due to Angular Testing Library @marcus-sa ?
I tried to reproduce this in #423 but everything seems fine.

Hi @timdeschryver @marcus-sa .
Will you please share what exactly caused this error.
I recently received this error too but unable to figure out what caused this error.

I am also receiving this error and also canno work out what causes it. Anybody find out?

Feel free to create a new issue with a reproduction if this error still occurs on the latest version (15.1.0).

I have the same issue with Angular v17
In my case it was obvious.
In ngAfterViewInit() I have used setTimeout(this.trackFormChanges(), 1000),
And if the user immideately leaved the component, then when the Macrotask(setTimeout) finally worked, it realized the component has already be destroyed, and cant find the ref. Bellow is the full example.

` ngAfterViewInit() {
setTimeout(() => {
this.formValueChanges();
}, 1000)
}

private formValueChanges() {
this.formGroup.valueChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.postChanged.emit(this.getPostFromForm() as unknown as PostModel);
})
}`

But when I removed the setTimeout it doesn't throw this error anymore NG0911: View has already been destroyed