@@ -92,12 +92,24 @@ function configureTestBed() {
92
92
testingBrowser . platformBrowserDynamicTesting ( )
93
93
) ;
94
94
95
- applyAngularTestingPatches ( testBed ) ;
95
+ patchTestBedToDestroyFixturesAfterEveryTest ( testBed ) ;
96
96
} ) ;
97
97
}
98
98
99
- /** Applies patches and workarounds to Angular's testing package. */
100
- function applyAngularTestingPatches ( testBed ) {
99
+ /**
100
+ * Monkey-patches TestBed.resetTestingModule such that any errors that occur during component
101
+ * destruction are thrown instead of silently logged. Also runs TestBed.resetTestingModule after
102
+ * each unit test.
103
+ *
104
+ * Without this patch, the combination of two behaviors is problematic for Angular Material:
105
+ * - TestBed.resetTestingModule catches errors thrown on fixture destruction and logs them without
106
+ * the errors ever being thrown. This means that any component errors that occur in ngOnDestroy
107
+ * can encounter errors silently and still pass unit tests.
108
+ * - TestBed.resetTestingModule is only called *before* a test is run, meaning that even *if* the
109
+ * aforementioned errors were thrown, they would be reported for the wrong test (the test that's
110
+ * about to start, not the test that just finished).
111
+ */
112
+ function patchTestBedToDestroyFixturesAfterEveryTest ( testBed ) {
101
113
// Original resetTestingModule function of the TestBed.
102
114
var _resetTestingModule = testBed . resetTestingModule ;
103
115
@@ -114,9 +126,9 @@ function applyAngularTestingPatches(testBed) {
114
126
}
115
127
} ;
116
128
117
- // Angular's testing package resets the testing module before each test. This is bad because
118
- // it doesn't allow developers to see what test actually failed. Fixing this by resetting
119
- // the testing module after each test.
129
+ // Angular's testing package resets the testing module before each test. This doesn't work well
130
+ // for us because it doesn't allow developers to see what test actually failed.
131
+ // Fixing this by resetting the testing module after each test.
120
132
// https://github.com/angular/angular/blob/master/packages/core/testing/src/before_each.ts#L25
121
133
afterEach ( function ( ) {
122
134
testBed . resetTestingModule ( ) ;
0 commit comments