Unit test error: Cannot call Promise.then from within a sync test

Move your variable initialization inside a beforeEach.

You shouldn’t be getting things out of the TestBed or managing the fixture or component in the describe scope. You should only do these things within the scope of a test run: inside a beforeEach/beforeAll, afterEach/afterAll, or inside an it.

describe(("test input "), () => {
  let comp: ToDoComponent;
  let fixture: ComponentFixture<ToDoComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [ToDoComponent],
        imports: [FormsModule]
      })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ToDoComponent);
    comp = fixture.componentInstance;
    de = fixture.debugElement.query(By.css("h1"));
    el = de.nativeElement;
  })


  it('should display a different test title', () => {
    comp.pageTitle="Test Title";
    fixture.detectChanges();
    expect(el.textContent).toBe('Test Title423');
  });

});

See also

  • https://angular.io/docs/ts/latest/guide/testing.html#!#waiting-compile-components

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)