31

I am testing an Angular2 component and want to assert nativeElement's property of the component, but there is no typescript definition for it. My test looks like this:

beforeEach( () => {
    myComponentFixture = TestBed.createComponent(MyComponent);
    myComponent = myComponentFixture.componentInstance;
});

it('Should display something', fakeAsync(() => {
    myComponentFixture.detectChanges();

    expect(myComponentFixture.nativeElement.textContent).toContain('something');
}));

The problem is that after i type nativeElement. there is not IntelliSense for it because I think there are no typings for nativeElement. There are more properties which I may want to check like innerHtml, id, etc. This example test may not make sense, but I may test some specific DOM element's properties using myComponentFixture.debugElement.query(By.css('#myElement')).nativeElement

Dainius Lukša
  • 966
  • 1
  • 11
  • 22

1 Answers1

49

You need to cast. Because of the multi-platform strategy, they didn't specify a specific type for nativeElement:

(myComponentFixture.nativeElement as HTMLElement)....
Dainius Lukša
  • 966
  • 1
  • 11
  • 22
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567