您如何知道单元测试时要导入哪些组件?

dan*_*y74 4 unit-testing karma-runner angular

我正在使用Angular2(2.1.0)最终版本.

我在使用单元测试时通过AppModule导入所有组件...

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [AppModule],
      ...
Run Code Online (Sandbox Code Playgroud)

但是,这使得测试运行缓慢.

我现在只列出我需要的组件如下......

  beforeEach(async(() => {
    // noinspection JSUnusedGlobalSymbols
    TestBed.configureTestingModule({
      imports: [BrowserModule, FormsModule, HttpModule], // modules
      declarations: [
        // pipes
        AttributeCheckPipe,
        // directives
        // DatePickerDirective,
        ...
Run Code Online (Sandbox Code Playgroud)

但是,我有很多很多组件,我不确定要导入哪些组件.测试输出不告诉我需要导入哪些.它只是简单地传递(当我全部导入它们)或失败(如果我没有)但它没有告诉我需要哪些.

这个错误很烦人/无用..

invokeTask@node_modules/zone.js/dist/zone.min.js:1:36996
onInvokeTask@node_modules/zone.js/dist/proxy.min.js:1:2190
invokeTask@node_modules/zone.js/dist/zone.min.js:1:36939
runTask@node_modules/zone.js/dist/zone.min.js:1:31466
a@node_modules/zone.js/dist/zone.min.js:1:17818
g@node_modules/core-js/client/shim.min.js:8:19058
node_modules/core-js/client/shim.min.js:8:19180
k@node_modules/core-js/client/shim.min.js:8:14294
l@node_modules/zone.js/dist/zone.min.js:1:18418
l@node_modules/zone.js/dist/zone.min.js:1:18175
node_modules/zone.js/dist/zone.min.js:1:18715
Run Code Online (Sandbox Code Playgroud)

如何获得有关我无法导入哪些组件的反馈?谢谢

我正在使用Karma和PhantomJS.

我的Karma配置摘录是..

client: {
  captureConsole: true
},
logLevel: config.LOG_DEBUG
Run Code Online (Sandbox Code Playgroud)

dan*_*y74 5

最后在这里取得了一些进展 我在compileComponents()中添加了一个catch块并记录了e.message并获得了一些有用的输出,这给了我一些工作!

继承我的代码..

  beforeEach(async(() => {
    TestBed.configureTestingModule({

      imports: [FormsModule, HttpModule, routing], // modules

      declarations: [
        SaveSearchModalComponent
      ],
      providers: [
        ESQueryService,
        RESTQueryService,
      ]
    }).compileComponents()
      .then(() => {
        fix = TestBed.createComponent(SaveSearchModalComponent);
        instance = fix.componentInstance;
        injector = fix.debugElement.injector;
      }).catch((e) => {
      console.log(e.message);
      throw e;
    });
  }));
Run Code Online (Sandbox Code Playgroud)

错误消息输出的摘录是......

'dynamic-form'不是已知元素:1.如果'dynamic-form'是Angular组件,则验证它是否是该模块的一部分.2.如果'dynamic-form'是Web组件,则将"CUSTOM_ELEMENTS_SCHEMA"添加到此组件的'@NgModule.schemas'以禁止显示此消息.

令人惊讶的是,文档中的任何地方都没有涉及(但我应该尽快猜到这一点!)

现在什么...

哇,在完成上述操作后(修复了99%的问题)我发现了另一个无用的错误信息......

组件e不是任何NgModule的一部分,或者模块尚未导入模块.

哪个来自......

/node_modules/@angular/compiler/bundles/compiler.umd.js

所以按照...的建议

Angular 2 Component不是任何NgModule的一部分

我将此日志语句添加到compiler.umd.js

// I ADDED THIS LOG STATEMENT
console.log('compType', String(compType));
// THIS LINE EXISTS ALREADY
throw new Error("Component " + stringify(compType) + " is not part of any NgModule or the module has not been imported into your module.");
Run Code Online (Sandbox Code Playgroud)

这通常是罪魁祸首.但是,在这里我得到了虚假输出......

日志:'function e(e){__ cov_m4LFTxiG42jWqZk7He0hiA.f ['4'] ++; __ cov_m4LFTxiG42jWqZk7He0hiA.s ['11'] ++; this.router = e,this.formErrors = {invalidCreds:!1};}'

提到这个.路人

所以我删除了路由导入,瞧!

但令人难以置信的是,这种痛苦是必要的.