title: Angular 8 unit test directive which includes setTimeout
body: <p>I have the following simple autofocus directive:</p>
  
  <pre><code>@Directive({
    selector: '[appAutoFocus]',
  })
  export class AutofocusDirective implements AfterContentInit {
  
    @Input() public appAutoFocus: boolean;
  
    public constructor(private el: ElementRef) { }
  
    public ngAfterContentInit() {
      if (this.appAutoFocus) {
        setTimeout(() =&gt; {
          this.el.nativeElement.focus();
        }, 300);
      }
    }
  }
  </code></pre>
  
  <p>I'm now trying to write some simple unit tests, but 2 out of the 3 tests fail.</p>
  
  <pre><code>@Component({
    template: '&lt;input type="text" [appAutoFocus]="true" /&gt;'
  })
  class TestComponent {
    constructor() { }
  }
  
  fdescribe('AutoFocusDirective', () =&gt; {
    let component: TestComponent;
    let fixture: ComponentFixture&lt;TestComponent&gt;;
    let inputEl: DebugElement;
       beforeEach(() =&gt; {
          TestBed.configureTestingModule({
            declarations: [
              TestComponent,
              AutofocusDirective
            ]
          });
  
          fixture = TestBed.createComponent(TestComponent);
          component = fixture.componentInstance;
          inputEl = fixture.debugElement.query(By.css('input'));
  
          spyOn(inputEl.nativeElement, 'focus');
          fixture.detectChanges();
        });
  
        it('should create an instance', () =&gt; {
          expect(component).toBeTruthy();
        });
  
        it('should call the focus event', fakeAsync(() =&gt; {
          tick(400);
          fixture.detectChanges();
          fixture.whenStable().then(() =&gt; {
           expect(inputEl.nativeElement.focus).toHaveBeenCalled();
          });
        }));
  
        it('should autofocus the input control', () =&gt; {
          const debugEl: DebugElement = fixture.debugElement;
          expect(debugEl.query(By.css('input:focus'))).not.toBe(null);
        });
  </code></pre>
  
  <p>"Should call the focus event" fails with <code>Spec 'AutoFocusDirective should call the focus event' has no expectations.</code></p>
  
  <p>"Should autofocus the input control" fails with <code>Expected null not to be null</code></p>