Skip to content

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

Merged

Conversation

devversion
Copy link
Member

@devversion devversion commented Jun 9, 2017

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).

@devversion devversion requested a review from jelbourn June 9, 2017 00:06
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 9, 2017
@devversion devversion force-pushed the fix/datepicker-destroy-error branch from 8f1f428 to 3b49661 Compare June 9, 2017 00:13
@@ -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());
Copy link
Member

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

}

/** Applies patches and workarounds to Angular's testing package. */
function applyAngularTestingPatches(testBed) {
Copy link
Member

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() {
}

Copy link
Member Author

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.

function applyAngularTestingPatches(testBed) {
// Original resetTestingModule function of the TestBed.
var _resetTestingModule = testBed.resetTestingModule;

Copy link
Member

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');
}

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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

}
};

// Angular's testing package resets the testing module before each test. This is bad because
Copy link
Member

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"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense.

@jelbourn
Copy link
Member

jelbourn commented Jun 9, 2017

Also the name of the PR / commit should reflect the content of the karma-shim change

@devversion
Copy link
Member Author

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 chore: now.

@devversion devversion force-pushed the fix/datepicker-destroy-error branch from 3b49661 to 324b926 Compare June 9, 2017 18:08
 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).
@devversion devversion force-pushed the fix/datepicker-destroy-error branch from 324b926 to 8cde483 Compare June 9, 2017 18:11
@devversion devversion changed the title fix(datepicker): rxjs throws error on component destroy build: report ngOnDestroy exceptions in unti tests Jun 9, 2017
@devversion
Copy link
Member Author

Addressed feedback.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Jun 9, 2017
@andrewseguin andrewseguin merged commit 21f764c into angular:master Jun 9, 2017
@devversion devversion deleted the fix/datepicker-destroy-error branch November 11, 2017 10:23
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants