-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(core): add opt-in test module teardown configuration #42566
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
Conversation
expect(fixtureDocument.body.contains(fixture.nativeElement)).toBe(false); | ||
}); | ||
|
||
it('should re-throw errors that were thrown during fixture cleanup', () => { |
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.
One note here: the original design only called for test module errors to be re-thrown, not test component, but I decided to make this change, because I was looking at how the Components repo is working around this and I realized that not throwing errors during test component destruction can still lead to difficult to trace down bugs. For reference: https://github.com/angular/components/blob/master/test/karma-test-shim.js#L61
0a5748c
to
dd2cb6e
Compare
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.
Looks great, thanks @crisbeto! I've just left a few comments, please have a look when you get a chance.
* @publicApi | ||
*/ | ||
export interface ModuleTeardownOptions { | ||
/** Whether the test module should be destroyed after every test. */ |
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.
It'd be great to mention a default value here too.
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 avoid mentioning default values in docs, because they inevitably go out of sync when the default changes.
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.
Ok, got it. I just noticed that you've added the default value description to the rethrowErrors
field, but not this one.
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 anticipate for the rethrowErrors
default value to change, whereas the teardown one will.
} | ||
|
||
/** | ||
* Object used to configure the test module teardown behavior in `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.
I think we'd need to add more info here to explain why these options exist and provide recommendations on when they should be used/changed and what the plan is (that these options will eventually be deprecated and removed, so we do not encourage using them unless it's absolutely needed and blocking tests).
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 idea was to have a silent rollout of this in 12.1.0 and then add a page in the docs similar to https://angular.io/guide/migration-update-libraries-tslib.
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.
Thanks for additional info, I'd propose adding a couple sentences to describe that:
* Object used to configure the test module teardown behavior in `TestBed`. | |
* Object used to configure the test module teardown behavior in `TestBed`. | |
* This option is exposed as a public API to allow switching to the legacy TestBed behavior temporarily | |
* (where testing module was not removed as a part of the test teardown process) in case your tests rely on | |
* this old behavior. These config options will be deprecated and removed in the future versions of Angular. |
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'm not sure that people who aren't familiar with the TestBed
internals would know what this is referring to.
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.
We can update the message to make it more user-friendly. I'd like to include some information about the fact that these config options are temporary and developers should not rely on them.
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.
IMO we should capture the fact that it's temporary when we eventually add the page on AIO. Also the plan is to provide schematics that add the parameter and then remove it once we've changed the default value so hopefully people won't have to deal with it themselves.
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 ModuleTeardownOptions
would become discoverable on the AIO once this PR is merged, see
https://pr42566-893884a.ngbuilds.io/api/core/testing/ModuleTeardownOptions.
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 meant that providing all the context behind the option would take a few paragraphs of text which seems like overkill for an API doc. E.g. the design doc for these changes takes 170 words to explain the issues.
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
dd2cb6e
to
b7a29d2
Compare
@AndrewKushnir I've addressed the feedback. @josephperrott I've pushed a fix for it. I had to revert one place where I tried to make something a couple lines shorter, but it ended up causing the error. |
b7a29d2
to
893884a
Compare
} | ||
|
||
// Otherwise use the configured behavior or default to rethrowing. | ||
return instanceOptions?.rethrowErrors ?? environmentOptions?.rethrowErrors ?? true; |
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.
Would it make sense to extract this default value to a const too (similar to TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT
)?
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 think the constant made sense for the teardown option since we're going to change it over time. I don't anticipate for the error rethrowing default behavior to change.
* @publicApi | ||
*/ | ||
export interface ModuleTeardownOptions { | ||
/** Whether the test module should be destroyed after every test. */ |
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.
Ok, got it. I just noticed that you've added the default value description to the rethrowErrors
field, but not this one.
} | ||
|
||
/** | ||
* Object used to configure the test module teardown behavior in `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.
Thanks for additional info, I'd propose adding a couple sentences to describe that:
* Object used to configure the test module teardown behavior in `TestBed`. | |
* Object used to configure the test module teardown behavior in `TestBed`. | |
* This option is exposed as a public API to allow switching to the legacy TestBed behavior temporarily | |
* (where testing module was not removed as a part of the test teardown process) in case your tests rely on | |
* this old behavior. These config options will be deprecated and removed in the future versions of Angular. |
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.
reviewed-for: public-api
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.
Reviewed-for: public-api
You can preview 0a272fd at https://pr42566-0a272fd.ngbuilds.io/. |
You can preview 0a272fd at https://pr42566-0a272fd.ngbuilds.io/. |
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 🍪
Reviewed-for: public-api
We currently have two long-standing issues related to how `TestBed` tests are torn down: 1. The dynamically-created test module isn't going to be destroyed, preventing the `ngOnDestroy` hooks on providers from running and keeping the component `style` nodes in the DOM. 2. The test root elements aren't going to be removed from the DOM. Instead, they will be removed whenever another test component is created. By themselves, these issues are easy to resolve, but given how long they've been around, there are a lot of unit tests out there that depend on the broken behavior. These changes address the issues by introducing APIs that allow users to opt into the correct test teardown behavior either at the application level via `TestBed.initTestEnvironment` or the test suite level via `TestBed.configureTestingModule`. At the moment, the new teardown behavior is opt-in, but the idea is that we'll eventually make it opt-out before removing the configuration altogether. Fixes angular#18831.
893884a
to
e8f32a2
Compare
Angular 12.1.0 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
Angular 12.1.0 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
Angular 12.1.0 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
In version 12.1, the framework added the `destroyAfterEach` an opt-in feature that improves tests performance and also addresses two long-standing issues. The idea, is to have this enabled by default in the future. Related PR: angular/angular#42566 Closes #21280
Angular 12.1.2 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
Angular 12.1.2 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
Angular 12.1.2 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead.
Angular 12.1.2 includes [opt-in automatic test module teardown](angular/angular#42566) which is similar to our logic of destroying the current fixture. These changes clean up our monkey patches and use the new option instead. (cherry picked from commit d090617)
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. |
We currently have two long-standing issues related to how
TestBed
tests are torn down:ngOnDestroy
hooks on providers from running and keeping the componentstyle
nodes in the DOM.By themselves, these issues are easy to resolve, but given how long they've been around, there are a lot of unit tests out there that depend on the broken behavior.
These changes address the issues by introducing APIs that allow users to opt into the correct test teardown behavior either at the application level via
TestBed.initTestEnvironment
or the test suite level viaTestBed.configureTestingModule
.At the moment, the new teardown behavior is opt-in, but the idea is that we'll eventually make it opt-out before removing the configuration altogether.
Fixes #18831.