-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: report ngOnDestroy exceptions in unti tests #5036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: report ngOnDestroy exceptions in unti tests #5036
Conversation
8f1f428
to
3b49661
Compare
src/lib/datepicker/datepicker.ts
Outdated
@@ -257,7 +257,7 @@ export class MdDatepicker<D> implements OnDestroy { | |||
this._ngZone.onStable.first().subscribe(() => this._popupRef.updatePosition()); | |||
} | |||
|
|||
this._popupRef.backdropClick().first().subscribe(() => this.close()); | |||
this._popupRef.backdropClick().subscribe(() => this.close()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should already be fixed in master
f89c6db
test/karma-test-shim.js
Outdated
} | ||
|
||
/** Applies patches and workarounds to Angular's testing package. */ | ||
function applyAngularTestingPatches(testBed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about patchTestModuleToDestroyFixturesAfterEveryTest
?
/**
* Monkey-patches TestBed.resetTestingModule such that any errors that occur during component
* destruction are thrown instead of silently logged. Also runs TestBed.resetTestingModule after
* each unit test.
*
* Without this patch, the combination of two behaviors is problematic for Angular Material:
* - TestBed.resetTestingModule catches errors thrown on fixture destruction and logs them without
* the errors ever being thrown. This means that any component errors that occur in ngOnDestroy
* can encounter errors silently and still pass unit tests.
* - TestBed.resetTestingModule is only called *before* a test is run, meaning that even *if* the
* aforementioned errors were thrown, they would be reported for the wrong test (the test that's
* about to start, not the test that just finished).
*/
function patchTestBedToDestroyFixturesAfterEveryTest() {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels a bit long but should be fine for the shims.
test/karma-test-shim.js
Outdated
function applyAngularTestingPatches(testBed) { | ||
// Original resetTestingModule function of the TestBed. | ||
var _resetTestingModule = testBed.resetTestingModule; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add something like...
if (!testBed._activeFixtures) {
throw Error('The monkey-patch for TestBed.resetTestingModule has failed ' +
'because a new version of Angular has removed the _activeFixtures member. ' +
'This needs to be fixed in karma-test-shim.js');
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary. If it will fail we will immediately see that the shims cause it to fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error that would be thrown is Cannot read property 'forEach' of undefined
. We have much more knowledge about this failure state, so we can help whoever is debugging this in the future by making a much more helpful message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but what I mean is that it's very unlikely that the _activeFixtures
property will be removed or whatever.
If it really comes to an error you will see the file that failed and the according line number.
Would be test/karma-test-shim.js:XXX
test/karma-test-shim.js
Outdated
} | ||
}; | ||
|
||
// Angular's testing package resets the testing module before each test. This is bad because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"This is bad" -> "This doesn't work well for us"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah makes sense.
Also the name of the PR / commit should reflect the content of the karma-shim change |
I only mentioned the datepicker because if it gets merged the changelog should only include that the datepicker bug was fixed. But since the fix is merged already it makes sense to make it a |
3b49661
to
324b926
Compare
Monkey-patches TestBed.resetTestingModule such that any errors that occur during component destruction are thrown instead of silently logged. Also runs TestBed.resetTestingModule after each unit test. Without this patch, the combination of two behaviors is problematic for Angular Material: - TestBed.resetTestingModule catches errors thrown on fixture destruction and logs them without the errors ever being thrown. This means that any component errors that occur in ngOnDestroy can encounter errors silently and still pass unit tests. - TestBed.resetTestingModule is only called *before* a test is run, meaning that even *if* the aforementioned errors were thrown, they would be reported for the wrong test (the test that's about to start, not the test that just finished).
324b926
to
8cde483
Compare
Addressed feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Monkey-patches TestBed.resetTestingModule such that any errors that occur during component destruction are thrown instead of silently logged. Also runs TestBed.resetTestingModule after each unit test.
Without this patch, the combination of two behaviors is problematic for Angular Material: