Skip to content

EffectRef.destroy has no impact #59410

Closed
Closed
@rainerhahnekamp

Description

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

I have a test that needs to ensure an effect is destroyed before it gets executed.
However, for some reason, calling destroy has no effect, and the test still passes.

Interestingly, if I call TestBed.flushEffects() before effectRef.destroy(),
the effectCounter remains at 1, as expected.

@JeanMeche suggested that once a run is scheduled, it cannot be stopped.
I get that, but in that case, the subsequent runs shouldn't happen.

it('should destroy an effect', () => {
  let effectCounter = 0;
  const counter = signal(1);
  const effectRef = TestBed.runInInjectionContext(() =>
    effect(
      () => {
        counter();
        effectCounter++;
      },
      { injector: TestBed.inject(EnvironmentInjector) }
    )
  );
  expect(effectCounter).toBe(0);
  effectRef.destroy();
  TestBed.flushEffects();
  expect(effectCounter).toBe(1);

  counter.set(2);
  TestBed.flushEffects();
  expect(effectCounter).toBe(2);

  counter.set(3);
  TestBed.flushEffects();
  expect(effectCounter).toBe(3);
});

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/github-6riblwis-lvalm53b?file=src%2Fapp%2Feffect.spec.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.0.6
Node: 22.5.1
Package Manager: pnpm 9.14.4
OS: darwin arm64

Angular: 19.0.5
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.6
@angular-devkit/build-angular   19.0.6
@angular-devkit/core            19.0.6
@angular-devkit/schematics      19.0.6
@angular/cli                    19.0.6
@schematics/angular             19.0.6
rxjs                            7.8.1
typescript                      5.6.3
zone.js                         0.15.0

Anything else?

No response

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimebugcore: reactivityWork related to fine-grained reactivity in the core framework

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions