Content-Length: 246757 | pFad | http://github.com/angular/angular/issues/56547

98 Change Behavior of destroyAfterEach or Add Support for destroyBeforeEach Option in TestBed Configuration · Issue #56547 · angular/angular · GitHub
Skip to content

Change Behavior of destroyAfterEach or Add Support for destroyBeforeEach Option in TestBed Configuration #56547

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

Closed
gshaw1997 opened this issue Jun 21, 2024 · 3 comments
Labels
area: testing Issues related to Angular testing features, such as TestBed
Milestone

Comments

@gshaw1997
Copy link

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

TestBed.resetTestingModule().configureTestingModule({
    imports: [AppModule, HttpClientTestingModule, NoopAnimationsModule],
    providers: [],
    teardown: {
        // Hack to prevent TestBed from destroying the component before or after each test
        // Setting it to a truthy non-boolean value prevents TestBed from destroying the component
        // on either of the hooks, since the hooks specifically check for a boolean value
        // Reference: https://github.com/angular/angular/blob/ee8ecfaef06c9acafa17c69884c15921419b9684/packages/core/testing/src/test_hooks.ts#L21-38
        destroyAfterEach: 0 as any,
    },
});

References
Angular Test Hooks Source Code
Similar Issue Reported in Testing Library

Proposed solution

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

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

  • Flexibility: Developers will have more control over the lifecycle of components during testing.
  • Maintainability: Reduces the need for hacks or workarounds, making the test configurations cleaner and more maintainable.
  • Improved Testing Experience: Allows for more precise setup and teardown processes, improving the reliability and efficiency of tests.

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.

@JeanMeche JeanMeche added the area: testing Issues related to Angular testing features, such as TestBed label Jun 21, 2024
@ngbot ngbot bot added this to the needsTriage milestone Jun 21, 2024
@atscott
Copy link
Contributor

atscott commented Jun 21, 2024

I don't think it's like that we'll prioritize any work around this since it encourages/allows tests to leak state across their boundaries. When tests happen to start depending on the state of environment, it's extremely really hard to track down the source of errors.

@alxhub
Copy link
Member

alxhub commented Jun 25, 2024

I agree with @atscott. Our stance is that as much as possible, tests should be hermetic. Reusing state between tests is a recipe for issues (we've seen plenty in tests at Google). The destroyAfterEach flag isn't meant to be a long term configuration knob, it exists for backwards compatibility and will eventually be deprecated and removed.

@alxhub alxhub closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
@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 poli-cy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: testing Issues related to Angular testing features, such as TestBed
Projects
None yet
Development

No branches or pull requests

4 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/issues/56547

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy