【问题标题】:Angular2 Karma test runner not reloadingAngular2 Karma 测试运行器未重新加载
【发布时间】:2017-08-23 07:41:32
【问题描述】:

我正在浏览 Angular2 测试教程。我对 Jasmine/Karma 很陌生,所以这可能是基本的。我注意到,一旦我运行“npm 测试”,如果我进行更改,测试运行程序会尝试重新加载但出现错误:

错误 C:/dev/unittest1/src/app/banner-inline/banner-inline.component.spec.ts (12,11): 找不到名称“HTMLElement”。)

这是规范代码(几乎来自here):

从 '@angular/core/testing' 导入 { ComponentFixture, TestBed, ComponentFixtureAutoDetect }; 从'@angular/platform-b​​rowser'导入{ By }; 从 '@angular/core' 导入 { DebugElement };

import { BannerInlineComponent } from './banner-inline.component';

describe('BannerInlineComponent (inline template)', () => {

  let comp: BannerInlineComponent;
  let fixture: ComponentFixture<BannerInlineComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [BannerInlineComponent], // declare the test component
      providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }]
    });

    fixture = TestBed.createComponent(BannerInlineComponent);

    comp = fixture.componentInstance; // BannerInlineComponent test instance

    // query for the title <h1> by CSS element selector
    de = fixture.debugElement.query(By.css('h1'));
    el = de.nativeElement;
  });

  it('should display original title', () => {
    expect(el.textContent).toContain(comp.title);
  });

  it('should still see original title after comp.title change', () => {
    const oldTitle = comp.title;
    comp.title = 'Test Title';
    fixture.detectChanges();
    // Displayed title is old because Angular didn't hear the change :(
    expect(el.textContent).toContain(oldTitle);
  });

  it('should display updated title after detectChanges', () => {
    comp.title = 'Test Title';
    fixture.detectChanges(); // detect changes explicitly
    expect(el.textContent).toContain(comp.title);
  });
});

如果我停止测试运​​行器并重新启动它,一切正常。为什么会出现此错误,我该如何预防?

【问题讨论】:

  • 您能否发布规范文件,它自身的错误表明它无法在规范文件中找到所需的属性,称为 HTML 元素,它可能基于某些触发器而出现需要查看规格文件
  • 我已经添加了规范代码。请记住,重新启动“npm test”是没有问题的。

标签: angular npm karma-runner karma-jasmine


【解决方案1】:

我猜你应该在项目中的 tsconfig.json 中将 dom 库添加到 lib 数组中:

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

【讨论】:

  • 我错过了 es2015.iterable 所以我添加了它,但它没有影响,我仍然收到错误。
  • @MikeWitt 您是否也对 tsconfig.spec.json 文件进行了更改
  • 我现在已经添加了。我还将它添加到 tsconfig.app.json 中。这些添加都没有帮助,我仍然得到错误。
  • 重新启动“npm test”,现在它工作正常。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
相关资源
最近更新 更多