如何将外部库包含到单元测试中:: Angular2-CLI

Nat*_*lia 1 javascript unit-testing jasmine karma-jasmine angular

我正在为我的项目编写测试用例。但不幸的是,在通过键入开始测试之后ng test,我收到以下错误:

Error: Error in :0:0 caused by: Plotly is
ReferenceError: Plotly is not defined
Run Code Online (Sandbox Code Playgroud)

Plotly是一个外部图表库。我试图在test文件中声明它:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MaterialModule } from '@angular/material';
import { WatchlistComponent } from './watchlist.component';
declare let Plotly: any;

describe('SystemComponent', () => {
  let component: WatchlistComponent;
  let fixture: ComponentFixture<WatchlistComponent>;
  let element;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ WatchlistComponent ],
      imports: [ FormsModule, MaterialModule ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(WatchlistComponent);
    component = fixture.componentInstance;
    element = fixture.nativeElement;
    fixture.detectChanges();
  });


  it('should return a positive number', () => {
    expect(component.getScreenHeight()).toMatch(/\d+/);
  });

});
Run Code Online (Sandbox Code Playgroud)

不幸的是,它仍然看不到Plotly并返回错误。功能的第一个测试案例getScreenHeight()甚至还没有启动。

也许茉莉花甚至在上传之前就开始测试了吗?

编辑:我Plotly通过包括<script src='plotly.js'></script>在我的index.html文件中导入。Plotly存储在本地相同的文件夹index.html中。

小智 5

负载plotly.js与适当的路径)文件到噶通过添加文件测试的env CONFIG在karma.config.js如下:

karma.config.js

config.set({
   ..
   files: ['plotly.js']
   ..
});
Run Code Online (Sandbox Code Playgroud)