question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing with BehaviourSubject and debounceTime.

See original GitHub issue

Hello 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Apr 29, 2021

That is because of completes the observable, which means that the debounceTime operator no longer awaits the timer as it certain there won’t be any additional values. This means that the switchMap callback is triggered synchronously, during the completion of the of observable.

0reactions
angular-automatic-lock-bot[bot]commented, May 30, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found