Testing with BehaviourSubject and debounceTime.
See original GitHub issueHello there! I cannot unit test a component which uses rxjs observable built with BehaviourSubject and which uses debounceTime.
Component example:
public gg = 1;
private subject = new BehaviorSubject("blabla");
private please = this.subject.asObservable();
// private please = of("blabla"); // it works
public wp$ = this.please.pipe(
debounceTime(300),
switchMap(() => {
this.gg = 2;
return of("new blabla");
})
);
Test example:
describe('test', () => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppModule]
})
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
it('works', fakeAsync(() => {
tick(300);
expect(component.gg).toBe(2);
}))
});
I subscribe to wp$
in html file using async pipe. The test does not pass. However, interestingly, if I change the source observable to something like of('blabla')
, i.e. observable which completed right away, the test would pass.
🔬 Full example reproduction
https://stackblitz.com/edit/angular-ivy-ue7wkw?file=src%2Fapp%2Fapp.component.ts
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to unit test Subject with a debounceTime in angular
I am not sure is this the correct way to test a subject.Can anyone tell me how to unit test the subject with...
Read more >Angular Unit Testing BehaviorSubject Karma Jamsine
How to Unit Test Behavior Subject in Angular?
Read more >How to test debounceTime in an angular ngrx effect using ...
3:29 - Starting to write the testsLinks mentioned- GitHub repository: https://github.com/ianjamieson/ngrx-effect-with-debounce- RxJS docs: ...
Read more >How I Write Marble Tests For RxJS Observables In Angular
From my experience, I can tell you that it is worth learning marble testing as you can then test very complex observable streams,...
Read more >Angular (forked) - StackBlitz
core/testing';. import {of, interval} from 'rxjs';. import { BehaviorSubject } from 'rxjs';. import { take, delay, debounceTime } from. 'rxjs/operators';.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
That is because
of
completes the observable, which means that thedebounceTime
operator no longer awaits the timer as it certain there won’t be any additional values. This means that theswitchMap
callback is triggered synchronously, during the completion of theof
observable.This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.