W3cubDocs

/Angular

fakeAsync

IMPORTANT: This API requires Zone.js and cannot be used with the Vitest test runner

API

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

Description

IMPORTANT: This API requires Zone.js and cannot be used with the Vitest test runner

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

Super-powered by Google ©2010–2025.
Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0.
https://angular.dev/api/core/testing/fakeAsync