Skip to content

Commit 324b926

Browse files
committed
Address feedback
1 parent a37520b commit 324b926

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

test/karma-test-shim.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,24 @@ function configureTestBed() {
9292
testingBrowser.platformBrowserDynamicTesting()
9393
);
9494

95-
applyAngularTestingPatches(testBed);
95+
patchTestBedToDestroyFixturesAfterEveryTest(testBed);
9696
});
9797
}
9898

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) {
101113
// Original resetTestingModule function of the TestBed.
102114
var _resetTestingModule = testBed.resetTestingModule;
103115

@@ -114,9 +126,9 @@ function applyAngularTestingPatches(testBed) {
114126
}
115127
};
116128

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.
120132
// https://github.com/angular/angular/blob/master/packages/core/testing/src/before_each.ts#L25
121133
afterEach(function() {
122134
testBed.resetTestingModule();

0 commit comments

Comments
 (0)