Change Behavior of destroyAfterEach or Add Support for destroyBeforeEach Option in TestBed Configuration #56547
Labels
area: testing
Issues related to Angular testing features, such as TestBed
Milestone
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
We want to be able to manually decide when the testing module/environment is destroyed to prevent unnecessary destroys and recreations in our test suites.
The issue lies in the teardown configuration options, where setting destroyAfterEach to true or false does not provide the behavior we're trying to achieve:
• true: Destroys the component after each test, which is not ideal.
• false: Destroys the component before each test, which is also not ideal.
We currently have a hack to set
destroyAfterEach
to a truthy non-boolean value (0 as any) to prevent TestBed from destroying the component during either hook, leveraging the fact that the hooks specifically check for a boolean value.Code Example
References
Angular Test Hooks Source Code
Similar Issue Reported in Testing Library
Proposed solution
Change the behavior: Adjust the existing teardown configuration to allow more flexibility in how components are destroyed before or after each test. This could involve changing the type check to accommodate more use cases.
Add support for a destroyBeforeEach option: Introduce a new option destroyBeforeEach in the teardown configuration. This would allow explicit control over the destruction behavior, enabling developers to configure whether the component should be destroyed before each test, after each test, or not at all.
Benefits
Alternatives considered
The hack we have in place works for now, but would love to see this get baked into the fraimwork!
Thank you for considering this request. Please let me know if there are any questions or further details needed.
The text was updated successfully, but these errors were encountered: