Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Testing "Cannot find name 'HTMLElement'"

I'm trying to introduce testing into my existing project, but I keep running into the same error, which I think is prohibiting me from running the tests. The error I get is

ERROR in .../src/app/dashboard.component.spec.ts (15,13): Cannot find name 'HTMLElement'.)

Chrome 57.0.2987 (Mac OS X 10.12.3): Executed 4 of 4 SUCCESS (7.24 secs / 6.55 secs)

My dashboard.component.spec.ts looks like this:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { DashboardComponent } from './dashboard.component';
import { ExchangeService } from './exchange.service';

describe('DashboardComponent (templateUrl)', () => {

    let comp: DashboardComponent;
    let fixture: ComponentFixture<DashboardComponent>;

    let spy: jasmine.Spy;
    let de: DebugElement;
    let el: HTMLElement; // how does this work...?
    let exchangeService: ExchangeService; // the actual injected service

    const testExchange = 'New York Stock Exchange';

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [ DashboardComponent ],
            providers: [ ExchangeService ],
        })
        .compileComponents();
    }))


    beforeEach(() => {

        fixture = TestBed.createComponent(DashboardComponent);
        comp = fixture.componentInstance; // DashboardComponent test componentInstance

        // ClockService actually injected into the component
        exchangeService = fixture.debugElement.injector.get(ExchangeService);

        // Setup spy on the `getExchanges` method
        spy = spyOn(exchangeService, 'getExchanges')
            .and.returnValue(Promise.resolve(testExchange));
        // query for the title <h1> by CSS element selector
        
        de = fixture.debugElement.query(By.css('.exchange'));
        el = de.nativeElement;
    });

    it('should not show exchange before OnInit', () => {
        expect(el.textContent).toBe('', 'nothing displayed');
        expect(spy.calls.any()).toBe(false, 'getExchanges not yet called');
    });

    it('true is true', () => expect(false).toBe(true));
});

I have searched all around for that error message, but I can't seem to find it anywhere. Also, the last test should fail, but I don't think it even compiles due to this error. Any clues as to what is wrong? I initially thought it would be an import that I was missing, but I can't see anyone else importing HTMLElements. Thanks!

Edit: My tsconfig.json looks like this:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "lib": [
      "es6",
      "dom",
      "es2015.iterable"
    ]
  }
}

Edit 2: I actually had to Ctrl+C, and restart the test for it to compile correctly (I thought it would do it upon change of files, but it didn't). Thank you!

like image 691
Christian Avatar asked Mar 20 '17 09:03

Christian


1 Answers

In your tsconfig.json from your test project you should add the dom library to the lib array:

"lib": ["es6", "dom", "es2015.iterable"],
like image 116
Poul Kruijt Avatar answered Sep 22 '22 00:09

Poul Kruijt