Skip to content

Commit df42d2b

Browse files
crisbetoalxhub
authored andcommitted
test: avoid leaking some LViews in tests (#57546)
We had some tests that were leaking LViews, because we were testing things like `createComponent`, but not destroying them afterwards. These changes clean up most of them, although there are a handful still left that I didn't have time to fully track down. PR Close #57546
1 parent 106917a commit df42d2b

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

packages/core/test/acceptance/change_detection_transplanted_view_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ describe('change detection for transplanted views', () => {
11051105
// goes and marks the root view dirty, which then starts the process all over again by
11061106
// checking the declaration.
11071107
expect(() => appRef.tick()).not.toThrow();
1108+
app.destroy();
11081109
});
11091110
it('does not cause infinite loops with exhaustive checkNoChanges', async () => {
11101111
TestBed.configureTestingModule({

packages/core/test/acceptance/component_spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ describe('component', () => {
738738
componentRef.instance.name = 'ZoneJS';
739739
componentRef.changeDetectorRef.detectChanges();
740740
expect(hostElement.textContent).toBe('Hello ZoneJS!');
741+
componentRef.destroy();
741742
});
742743

743744
it('should create an instance of an NgModule-based component', () => {
@@ -797,6 +798,7 @@ describe('component', () => {
797798

798799
componentRef.changeDetectorRef.detectChanges();
799800
expect(hostElement.innerHTML.replace(/\s*/g, '')).toBe('<p>1</p>|<p>2</p>|<p>3</p>');
801+
componentRef.destroy();
800802
});
801803

802804
it('should be able to inject tokens from EnvironmentInjector', () => {
@@ -817,6 +819,7 @@ describe('component', () => {
817819
componentRef.changeDetectorRef.detectChanges();
818820

819821
expect(hostElement.textContent).toBe('Token: EnvironmentInjector(A)');
822+
componentRef.destroy();
820823
});
821824

822825
it('should be able to use NodeInjector from the node hierarchy', () => {
@@ -890,6 +893,7 @@ describe('component', () => {
890893
expect(hostElement.tagName.toLowerCase()).toBe(selector);
891894

892895
expect(hostElement.textContent).toBe('Hello Angular!');
896+
componentRef.destroy();
893897
});
894898

895899
it(
@@ -917,6 +921,7 @@ describe('component', () => {
917921
expect(hostElement.tagName.toLowerCase()).toBe('div');
918922

919923
expect(hostElement.textContent).toBe('Hello Angular!');
924+
componentRef.destroy();
920925
},
921926
);
922927

packages/core/test/acceptance/content_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,7 @@ describe('projection', () => {
16681668
componentRef.changeDetectorRef.detectChanges();
16691669

16701670
expect(getElementHtml(hostElement)).toContain('<p>override</p>|Two fallback|Three fallback');
1671+
componentRef.destroy();
16711672
});
16721673

16731674
it('should render fallback content when ng-content is inside an ng-template', () => {

packages/core/test/acceptance/view_container_ref_spec.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ describe('ViewContainerRef', () => {
144144

145145
// Add a test component to the view container ref to ensure that
146146
// the "ng-container" comment was used as marker for the insertion.
147-
vcref.createComponent(HelloComp);
147+
const ref = vcref.createComponent(HelloComp);
148148
fixture.detectChanges();
149149

150150
expect(testParent.textContent).toBe('hello');
151151
expect(testParent.childNodes.length).toBe(2);
152152
expect(testParent.childNodes[0].nodeType).toBe(Node.ELEMENT_NODE);
153153
expect(testParent.childNodes[0].textContent).toBe('hello');
154154
expect(testParent.childNodes[1].nodeType).toBe(Node.COMMENT_NODE);
155+
ref.destroy();
155156
});
156157

157158
it('should support attribute selectors in dynamically created components', () => {
@@ -167,7 +168,7 @@ describe('ViewContainerRef', () => {
167168
@ViewChild('container', {read: ViewContainerRef}) vcRef!: ViewContainerRef;
168169

169170
createComponent() {
170-
this.vcRef.createComponent(HelloComp);
171+
return this.vcRef.createComponent(HelloComp);
171172
}
172173
}
173174

@@ -176,9 +177,10 @@ describe('ViewContainerRef', () => {
176177
fixture.detectChanges();
177178
expect(fixture.debugElement.nativeElement.innerHTML).not.toContain('Hello');
178179

179-
fixture.componentInstance.createComponent();
180+
const ref = fixture.componentInstance.createComponent();
180181
fixture.detectChanges();
181182
expect(fixture.debugElement.nativeElement.innerHTML).toContain('Hello');
183+
ref.destroy();
182184
});
183185

184186
it('should view queries in dynamically created components', () => {
@@ -294,11 +296,11 @@ describe('ViewContainerRef', () => {
294296
) {}
295297

296298
createComponentViaVCRef() {
297-
this.vcRef.createComponent(HelloComp);
299+
return this.vcRef.createComponent(HelloComp);
298300
}
299301

300302
createComponentViaFactory() {
301-
createComponent(HelloComp, {
303+
return createComponent(HelloComp, {
302304
environmentInjector: this.injector,
303305
hostElement: this.elementRef.nativeElement.querySelector('#factory'),
304306
});
@@ -308,8 +310,8 @@ describe('ViewContainerRef', () => {
308310
TestBed.configureTestingModule({declarations: [TestComp, HelloComp]});
309311
const fixture = TestBed.createComponent(TestComp);
310312
fixture.detectChanges();
311-
fixture.componentInstance.createComponentViaVCRef();
312-
fixture.componentInstance.createComponentViaFactory();
313+
const firstRef = fixture.componentInstance.createComponentViaVCRef();
314+
const secondRef = fixture.componentInstance.createComponentViaFactory();
313315
fixture.detectChanges();
314316

315317
// Verify host element for a component created via `vcRef.createComponent` method
@@ -336,6 +338,8 @@ describe('ViewContainerRef', () => {
336338
// Make sure selector-based attrs and classes were not added to the host element
337339
expect(factoryHostElement.classList.contains('class-a')).toBe(false);
338340
expect(factoryHostElement.getAttribute('attr-c')).toBe(null);
341+
firstRef.destroy();
342+
secondRef.destroy();
339343
});
340344
});
341345

@@ -1534,6 +1538,7 @@ describe('ViewContainerRef', () => {
15341538
fixture.componentInstance.viewContainerRef.createComponent(DynamicComponent);
15351539
const element = componentRef.location.nativeElement;
15361540
expect((element.namespaceURI || '').toLowerCase()).not.toContain('svg');
1541+
componentRef.destroy();
15371542
});
15381543

15391544
it('should be compatible with componentRef generated via TestBed.createComponent in component factory', () => {

packages/core/test/linker/regression_integration_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ describe('regressions', () => {
365365
});
366366

367367
expect(compRef.location.nativeElement.hasAttribute('ng-version')).toBe(false);
368+
compRef.destroy();
368369
});
369370
});
370371

packages/core/test/render3/reactivity_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe('effects', () => {
100100
),
101101
);
102102
await expectAsync(p).toBeResolvedTo([false, true]);
103+
componentRef.destroy();
103104
});
104105

105106
it('should propagate errors to the ErrorHandler', () => {
@@ -543,7 +544,7 @@ describe('effects', () => {
543544
const fixture = TestBed.createComponent(DriverCmp);
544545
fixture.detectChanges();
545546

546-
fixture.componentInstance.vcr.createComponent(TestCmp);
547+
const ref = fixture.componentInstance.vcr.createComponent(TestCmp);
547548

548549
// Verify that simply creating the component didn't schedule the effect.
549550
TestBed.flushEffects();
@@ -552,6 +553,7 @@ describe('effects', () => {
552553
// Running change detection should schedule and run the effect.
553554
fixture.detectChanges();
554555
expect(log).toEqual(['init', 'effect']);
556+
ref.destroy();
555557
});
556558

557559
it('when created in a service provided in a component', () => {

0 commit comments

Comments
 (0)