• Overview
@angular/core/testing

fakeAsync

function
stable

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.

API

function fakeAsync(
  fn: Function,
  options?: { flush?: boolean | undefined } | undefined,
): (...args: any[]) => any;

fakeAsync

(...args: any[]) => any

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.

Can be used to wrap inject() calls.

@paramfnFunction

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

@paramoptions{ flush?: boolean | undefined; } | undefined
  • flush: When true, will drain the macrotask queue after the test function completes. When false, will throw an exception at the end of the function if there are pending timers.
@returns(...args: any[]) => any

Description

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.

Can be used to wrap inject() calls.

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);
    }),
  );
});
Jump to details