|
| 1 | +import {NgModuleRef} from '@angular/core'; |
1 | 2 | import {ComponentFixture, TestBed} from '@angular/core/testing';
|
2 | 3 | import {
|
3 | 4 | BrowserDynamicTestingModule,
|
|
8 | 9 | * Common setup / initialization for all unit tests in Angular Material and CDK.
|
9 | 10 | */
|
10 | 11 |
|
11 |
| -const testBed = TestBed.initTestEnvironment( |
12 |
| - [BrowserDynamicTestingModule], platformBrowserDynamicTesting()); |
| 12 | +const testBed = |
| 13 | + TestBed.initTestEnvironment([BrowserDynamicTestingModule], platformBrowserDynamicTesting()); |
13 | 14 | patchTestBedToDestroyFixturesAfterEveryTest(testBed);
|
14 | 15 |
|
15 | 16 | (window as any).module = {};
|
@@ -38,9 +39,17 @@ function patchTestBedToDestroyFixturesAfterEveryTest(testBedInstance: TestBed) {
|
38 | 39 | // Monkey-patch the resetTestingModule to destroy fixtures outside of a try/catch block.
|
39 | 40 | // With https://github.com/angular/angular/commit/2c5a67134198a090a24f6671dcdb7b102fea6eba
|
40 | 41 | // 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}) { |
42 | 44 | try {
|
43 | 45 | 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 | + } |
44 | 53 | } finally {
|
45 | 54 | this._activeFixtures = [];
|
46 | 55 | // Regardless of errors or not, run the original reset testing module function.
|
|
0 commit comments