Test fails with flush() but works with tick()
See original GitHub issueπ bug report
Affected Package
The issue is caused by package @angular/core/testing
Is this a regression?
I dont know.
Description
I have a simple component:
import { Component, OnDestroy, OnInit } from '@angular/core';
import { asyncScheduler, Observable, of, queueScheduler, scheduled } from 'rxjs';
@Component({
selector: 'test-component',
templateUrl: './test-component.component.html'
})
export class TestComponentComponent implements OnInit {
value: string;
constructor() { }
ngOnInit(): void {
const data$ = this.fetchDataScheduler();
data$
.subscribe(value => {
this.value = value;
});
}
private fetchDataScheduler(): Observable<string> {
return scheduled(of('foo'), asyncScheduler);
}
}
and two simple tests:
Passing:
it('async - test setTimeout', fakeAsync(() => {
expect(component.value).toBeFalsy();
fixture.detectChanges(); // ngOnInit
expect(component.value).toBeFalsy();
tick();
expect(component.value).toBe('foo');
}));
Failing:
it('async - test setTimeout', fakeAsync(() => {
expect(component.value).toBeFalsy();
fixture.detectChanges(); // ngOnInit
expect(component.value).toBeFalsy();
flush();
expect(component.value).toBe('foo'); // <- fails here
}));
I think that the test with flush() should pass too, according to documentation:
Simulates the asynchronous passage of time for the timers in the fakeAsync zone by draining the macrotask queue until it is empty. The returned value is the milliseconds of time that would have been elapsed.
π¬ Minimal Reproduction
Stackblitz or GitHub
π Your Environment
Angular Version:
Angular CLI: 8.3.20
Node: 12.11.1
OS: win32 x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.802.2
@angular-devkit/build-angular 0.802.2
@angular-devkit/build-optimizer 0.802.2
@angular-devkit/build-webpack 0.802.2
@angular-devkit/core 8.2.2
@angular-devkit/schematics 8.3.20
@angular/cdk 8.2.3
@angular/cli 8.3.20
@angular/flex-layout 8.0.0-beta.27
@angular/material 8.2.3
@angular/material-moment-adapter 8.2.3
@ngtools/webpack 8.2.2
@schematics/angular 8.3.20
@schematics/update 0.803.20
rxjs 6.5.4
typescript 3.5.3
webpack 4.38.0
Anything else relevant? Discussed on SO
and here, where Netanel suggests that it is caused by periodic tasks not being flushed with flush().
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
What is the difference between tick() and flush() in angular ...
This unit test shows that all async tasks should be finished when it returns, and that the returned value tells you how long...
Read more >Angular testing: flush vs. flushMicrotasks - Damir's Corner
The test can be fixed by using flushMicrotasks instead of flush : it("should add a toast", fakeAsync(() => { const toastsService = TestBed....
Read more >API Reference | Vitest
skipIf(isDev)('prod only test', () => { // this test only runs in production }) ... stockFailed - it may return any falsy value,...
Read more >Asynchronous work - Jasmine Documentation
beforeEach(async function() { await someLongSetupFunction(); }); it('does a ... to be more error-prone, but it can be useful for testing callback-based codeΒ ...
Read more >fakeAsync - Angular
Timers are synchronous; tick() simulates the asynchronous passage of time. ... describe('this test', () => { it('looks async but is synchronous',Β ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@felikf , this is not a bug because in your code you use
asyncSchedulerwhich is in fact asetIntervalwhich is aperiodicmacroTask, and currently theflush()API only flush non-periodic macroTasks such assetTimeout(), so for now, please usetick()to make it work, and I am currently working on adding new APIs to make this work easily. Thanks.@JiaLiPassion is there any status update on the βnew APIsβ you mentioned in your comment above? rxjs
AsyncTaskis the default scheduler for a lot of observables, and because it usessetIntervalunder the hood β even for observables that run once β itβs currently impossible toflushthem.