加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2测试“找不到名字’HTMLElement’”

发布时间:2020-12-17 18:01:07 所属栏目:安全 来源:网络整理
导读:我正在尝试将测试引入我现有的项目中,但我仍然遇到同样的错误,我认为这会阻止我运行测试.我得到的错误是 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 o
我正在尝试将测试引入我现有的项目中,但我仍然遇到同样的错误,我认为这会阻止我运行测试.我得到的错误是

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)

我的dashboard.component.spec.ts看起来像这样:

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));
});

我已四处搜索该错误消息,但我似乎无法在任何地方找到它.此外,最后一次测试应该失败,但我认为它甚至不会因为这个错误而编译.关于什么是错的任何线索?我最初认为这将是一个我失踪的导入,但我看不到其他任何人导入HTMLElements.谢谢!

编辑:
我的tsconfig.json看起来像这样:

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

编辑2:
我实际上不得不按Ctrl C,并重新启动测试,以便正确编译(我认为它会在更改文件时执行,但它没有).谢谢!

解决方法

在测试项目的tsconfig.json中,您应该将dom库添加到lib数组中:

"lib": ["es6","es2015.iterable"],

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读