Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

fakeAsync

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