9

I do a one time subscription that has a debounceTime pipe on my component onInit:

this.subscriptions.add(
  this.updateJobs$
    .pipe(
      filter(jobs => !!jobs.length),
      debounceTime(MyComponent.ListStabilizationTimeInMS),
      first(),
    )
    .subscribe((jobs: Update[]) => {
      // some code
    })
);

Because of this I need to include discardPeriodicTasks(); as last line after each fakeAsync tests, which I find tedious. Otherwise I get the error Error: 1 periodic timer(s) still in the queue. for all the tests.

I tried to create an afterEach:

afterEach(()=> {
  discardPeriodicTasks();
});

afterEach(fakeAsync(()=> {
  discardPeriodicTasks();
}));

But it does not work and the only way to pass the tests is to manually append discardPeriodicTasks(); to all the test cases. Would be great if it could be automated.

ruth
  • 29,535
  • 4
  • 30
  • 57
distante
  • 6,438
  • 6
  • 48
  • 90
  • Could you post your whole component code along with the test setup (`TestBed...`) and tests in question? – Philipp Meissner Feb 11 '20 at 05:48
  • Hi @PhilippMeissner I am not using TestBed, I am doing unit tests without Angular environment `sut = new MyComponent(mockedProdiver1, mockedProdiver2, mockedProdiver3)` – distante Feb 11 '20 at 06:56
  • Still, the entire code base to the problem would help a lot to potentially identify an issue. – Philipp Meissner Feb 11 '20 at 07:52
  • I had the same error with `fakeAsync` and it seems that this error("Error: 1 periodic timer(s) still in the queue.") only caused when a test fails. Try to run a test with success and take a look if an error persists. – andriishupta Feb 11 '20 at 09:43
  • @andriishupta I have a lot of test there, all correct and passing. After I add `debounceTime` they break. – distante Feb 11 '20 at 15:42
  • 1
    Because they don't use a virtual testing timer. Such as I don't see test code, so: are you sure tick/flush, etc is used correctly? also, have found something that might help you - clearing the timer manually: https://stackoverflow.com/questions/57093935/how-to-unit-test-a-function-is-called-correctly-after-a-timer-has-fired-angular – andriishupta Feb 11 '20 at 17:46
  • @andriishupta I can not paste the code because it is sadly proprietary.. I know that a flush of the periodic task is needed I am just looking for a way to do always for each tests. – distante Feb 11 '20 at 19:52
  • 2
    @distante can you at least create a dummy component and replicate issue somewhere and share? – Dipen Shah Feb 13 '20 at 01:28

0 Answers0