Giter Club home page Giter Club logo

Comments (2)

atscott avatar atscott commented on April 30, 2024

I actually misunderstood this before and it's working as expected today. The TL;DR is kind of that you're using autoDetect wrong in this case. You should be waiting for the fixture stableness rather than autoDetect to cause stableness to be applied synchronously. Using your test component above, this works:

 beforeEach(async () => {
    await TestBed.configureTestingModule({
      providers: [{provide: ComponentFixtureAutoDetect, useValue: true}]
    }).compileComponents();
  });

  it('should create the app', async () => {
   @Component({template: '{{title()}}'})
   class Example {
     title = signal('');
     constructor() {
       effect(() => {
         this.title.set('demo');
       }, {allowSignalWrites: true});
     }
   }
   
   const fixture = TestBed.createComponent(Example);
   await fixture.whenStable();
   expect(fixture.nativeElement.textContent).toEqual('demo'); // ✅
  });

from angular.

yjaaidi avatar yjaaidi commented on April 30, 2024

Thanks @atscott, I totally agree with you. ComponentFixture#whenStable() is indeed a way better option.

I think that my issue description was not very clear.
What I wanted to highlight is that there was a behavior change since the change of effect scheduling in v17.1.0 (1f8c53c)
Since effects became scheduled as microtasks, tests that relied on synchronous change detection using ComponentFixture#detectChanges were broken.

For example, Angular Testing Library currently relies on sync change detection instead of ComponentFixture#whenStable() (cf. https://github.com/testing-library/angular-testing-library/blob/main/projects/testing-library/src/lib/testing-library.ts#L253)
It would be wise to switch to whenStable() but then it raises another issue. There should be an opt-out when combined with fakeAsync as calling whenStable() with any pending timers in fakeAsync will produce the following error: timer(s) still in the queue. if we want to make assertions before flushing the timers.

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.