Does fixture.whenStable() actually do anything in

2020-07-10 07:08发布

问题:

I have been reading a lot about angular testing lately and the pairs are always async+fixture.whenStable and fakeAsync+tick, however you can always call fixtrue.whenStable as it is not tightly coupled. If you call it when not using the async utility to track promises in a test zone will it actually do anything?

For example:

it('should be able to do this thing that needs some async setup', () => {
            fixture.detectChanges();
            fixture.whenStable().then()
});

I understand the difference between FakeAsync/Tick and fixture.detectChanges. My question is regarding what fixture.whenstable will do when inside of a FakeAsync execution zone as an Async zone should keep track of async work allowing fixture.whenstable to hook into that tracking, at least as I understand it. Or indeed if used and not within an async execution zone at all.

So if fixture.whenstable is used inside a FakeAsync function, or a function that does not setup an async execution zone, will it work as intended?

回答1:

No the whenStable() does nothing if you test without async of fakeAsync. What whenStable() does is to wait for all tasks in the test NgZone to complete. When you don't test with async the NgZone does not get created at all and whenStable() just returns immediately.

If you want more detail check the code for ComponentFixture in GitHub.