Angular testing: tick vs flushMicrotasks in fakeAsync block
Asked Answered
A

1

7

As far as my understanding goes from reading the Angular testing docs, calling tick() flushes both (supported) macro tasks, and micro-task queues within the fakeAsync block. In which case, under the hood, I assume, calling tick() will be the same as having some additional calls + calling flushMicrotasks().

Question is, is there any case where I should use:

it('should pass', fakeAsync(() => {
  // given some setup...

  doSomethingAsynchronous();
  flushMicrotasks();

  // do some assertions...
}));

instead of

it('should pass', fakeAsync(() => {
  // given some setup...

  doSomethingAsynchronous();
  tick();

  // do some assertions...
}));

Augustaugusta answered 16/9, 2018 at 22:41 Comment(0)
D
5

Excerpt from the article here.

macrotasks are enqueued by setTimeout, setInterval, setImmediate, etc. microtasks by process.nextTick, Promises, MutationObserver, etc.

So if you are using setTimeouts, setInterval etc then use tick() and if you are using some promises, then you can use either tick() or flushMicrotasks().

Dermoid answered 27/2, 2019 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.