javascript - Angular2 + karma 测试 : “Illegal state: Could not load the summary for directive Foo.”

标签 javascript angular karma-runner karma-jasmine angular-cli

我有一个看起来像这样的组件:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-youtube',
  templateUrl: './youtube.component.html',
  styleUrls: ['./youtube.component.css']
})
export class YoutubeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  } 

}

在我的 karma 规范中,我有以下内容:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { YoutubeComponent } from './youtube.component';

describe('YoutubeComponent', () => {
  let component: YoutubeComponent;
  let fixture: ComponentFixture<YoutubeComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      // providers: [YoutubeComponent],tried this, and it makes no difference
      declarations: [ YoutubeComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(YoutubeComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

我在 Karma 浏览器中遇到的错误如下所示:

Error: Illegal state: Could not load the summary for directive YouTubeComponent.

似乎previous, related questions通过添加行 declarations: [ YoutubeComponent ] 来解决,我已经有了。还有其他建议吗?

github 上有一个可重现的例子:

git clone https://github.com/Atticus29/dataJitsu.git
cd dataJitsu
git checkout modalSO
rm package-lock.json
npm install
npm test

最佳答案

@Atticus29 通常在您错过以下操作时会出现此错误:

  1. 声明一些相关组件
  2. 在导入中添加一些引用的模块

尝试检查任何引用的组件或模块。

关于javascript - Angular2 + karma 测试 : “Illegal state: Could not load the summary for directive Foo.” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603526/

相关文章:

javascript - 如何将javascript数据添加到mysql?

javascript - asp.net/jQuery 全选仅适用于选项卡内

import - 用于 Observable 的 Angular 2 导入共享运算符

angularjs - 尝试使用 templateURL 测试 AngularJS 指令

angular - 在规范文件中声明组件,而不是将顶级模块加载到 TestBed 中

javascript - 替换 HTML 元素中的字符

angular - 如何在没有 TypeError : Cannot read property 'create' of undefined? 的情况下执行 ng-xi18n

angular - 在索引 0 处初始化没有空输入的表单数组

cordova - 在 Cordova 项目上运行 Karma 时执行 0 of 0 ERROR

javascript - mxGraph:来自外部按钮的单击事件