Is there typescript definition for DOM nativeElement?

Dainius Lukša picture Dainius Lukša · Dec 8, 2016 · Viewed 8.6k times · Source

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

Answer

Günter Zöchbauer picture Günter Zöchbauer · Dec 8, 2016

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

(myComponentFixture.nativeElement as HTMLElement)....