W3cubDocs

/Angular

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

The function that you want to wrap in the fakeAsync zone.

Returns

(...args: any[]) => any: The function wrapped to be executed in the fakeAsync zone. Any arguments passed when calling this returned function will be passed through to the fn function in the parameters when it is called.

Description

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

Can be used to wrap inject() calls.

Further information is 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–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/core/testing/fakeAsync