W3cubDocs

/Angular 11

fakeAsync

function

Wraps a function to be executed in the fakeAsync zone:

  • microtasks are manually executed by calling flushMicrotasks(),
  • timers are synchronous, tick() simulates the asynchronous passage of time.

See more...

fakeAsync(fn: Function): (...args: any[]) => any

Parameters
fn Function
Returns

(...args: any[]) => any: The function wrapped to be executed in the fakeAsync zone

Description

If there are any pending timers at the end of the function, an exception will be thrown.

Can be used to wrap inject() calls.

Further information available in the Usage Notes...

Usage notes

Example

describe('this test', () => {
  it('looks async but is synchronous', <any>fakeAsync((): void => {
       let flag = false;
       setTimeout(() => {
         flag = true;
       }, 100);
       expect(flag).toBe(false);
       tick(50);
       expect(flag).toBe(false);
       tick(50);
       expect(flag).toBe(true);
     }));
});

© 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v11.angular.io/api/core/testing/fakeAsync