angular / angular

Deliver web apps with confidence 🚀
https://angular.dev
MIT License
96.15k stars 25.44k forks source link

TestBed.flushEffects does not work if the Injector passed to the effect came from a ComponentFixture #58452

Open DDtMM opened 4 days ago

DDtMM commented 4 days ago

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

core

Is this a regression?

Yes

Description

In Angular 18, I had tests that passed the Injector from a ComponentFixture down to an effect inside a custom signal. When upgrading to Angular 19 these tests broke. Here's a simple example:

describe('customSignal', () => {
  let fixture: ComponentFixture<unknown>;
  let injector: Injector;

  beforeEach(() => {
    fixture = createFixture();
    injector = fixture.componentRef.injector;
  });

  fit('should do something', () => {
    const sut = customSignal(5, { injector });
    sut.set(6);
    TestBed.flushEffects(); // Used to work in Angular 19.
    expect(sut()).toBe(24); // Expectation fails because an update that was supposed to be made by an internal effect didn't occur.
  });
});

There are several work arounds:

Please provide a link to a minimal reproduction of the bug

https://github.com/DDtMM/angular-signal-generators/blob/f33e82ea46ae3e2160133c67c62503fc2d405c39/projects/signal-generators/src/lib/signals/async-signal.spec.ts#L69

Please provide the exception or error you saw

The changes that should occur after using TestBed.flushEffects() do not occur and my test expectations fail.

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

Angular CLI: 19.0.0-rc.0 Node: 20.14.0 Package Manager: npm 10.3.0 OS: win32 x64

Angular: 19.0.0-rc.0 ... animations, cli, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, platform-server ... router, ssr

Package Version

@angular-devkit/architect 0.1900.0-rc.0 @angular-devkit/build-angular 19.0.0-rc.0 @angular-devkit/core 19.0.0-rc.0 @angular-devkit/schematics 19.0.0-rc.0 @angular/cdk 19.0.0-next.10 @schematics/angular 19.0.0-rc.0 ng-packagr 19.0.0-next.7 rxjs 7.8.1 typescript 5.6.3 zone.js 0.15.0

Anything else?

No response

alxhub commented 4 days ago

This is working as intended/expected - TestBed.flushEffects is (now) only handling "root" effects. We will likely remove this API at some point, since effects now run via the scheduler (even in tests).