Skip to content

Commit a5ced9f

Browse files
committed
build: do not leak component styles between tests
1 parent 4e2decb commit a5ced9f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

test/angular-test-init-spec.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {NgModuleRef} from '@angular/core';
12
import {ComponentFixture, TestBed} from '@angular/core/testing';
23
import {
34
BrowserDynamicTestingModule,
@@ -8,8 +9,8 @@ import {
89
* Common setup / initialization for all unit tests in Angular Material and CDK.
910
*/
1011

11-
const testBed = TestBed.initTestEnvironment(
12-
[BrowserDynamicTestingModule], platformBrowserDynamicTesting());
12+
const testBed =
13+
TestBed.initTestEnvironment([BrowserDynamicTestingModule], platformBrowserDynamicTesting());
1314
patchTestBedToDestroyFixturesAfterEveryTest(testBed);
1415

1516
(window as any).module = {};
@@ -38,9 +39,17 @@ function patchTestBedToDestroyFixturesAfterEveryTest(testBedInstance: TestBed) {
3839
// Monkey-patch the resetTestingModule to destroy fixtures outside of a try/catch block.
3940
// With https://github.com/angular/angular/commit/2c5a67134198a090a24f6671dcdb7b102fea6eba
4041
// errors when destroying components are no longer causing Jasmine to fail.
41-
testBedInstance.resetTestingModule = function(this: {_activeFixtures: ComponentFixture<any>[]}) {
42+
testBedInstance.resetTestingModule = function(
43+
this: {_activeFixtures: ComponentFixture<any>[], _testModuleRef: NgModuleRef<unknown>|null}) {
4244
try {
4345
this._activeFixtures.forEach((fixture: ComponentFixture<any>) => fixture.destroy());
46+
// Destroy the TestBed `NgModule` reference to clear out shared styles that would
47+
// otherwise remain in DOM and significantly increase memory consumption in browsers.
48+
// This increased consumption then results in noticeable test instability and slow-down.
49+
// See: https://github.com/angular/angular/issues/31834.
50+
if (this._testModuleRef !== null) {
51+
this._testModuleRef.destroy();
52+
}
4453
} finally {
4554
this._activeFixtures = [];
4655
// Regardless of errors or not, run the original reset testing module function.

test/karma-test-shim.js

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ function patchTestBedToDestroyFixturesAfterEveryTest(testBed) {
8080
testBed.resetTestingModule = function() {
8181
try {
8282
this._activeFixtures.forEach(function (fixture) { fixture.destroy(); });
83+
// Destroy the TestBed `NgModule` reference to clear out shared styles that would
84+
// otherwise remain in DOM and significantly increase memory consumption in browsers.
85+
// This increased consumption then results in noticeable test instability and slow-down.
86+
// See: https://github.com/angular/angular/issues/31834.
87+
if (this._testModuleRef !== null) {
88+
this._testModuleRef.destroy();
89+
}
8390
} finally {
8491
this._activeFixtures = [];
8592
// Regardless of errors or not, run the original reset testing module function.

0 commit comments

Comments
 (0)