简体   繁体   English

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

[英]How to include external library into unit tests::Angular2-CLI

I'm writing test cases for my project. 我正在为我的项目编写测试用例。 But unfortunately, after starting the tests by typing ng test , I'm receiving following error: 但是不幸的是,在通过键入ng test开始测试之后,我收到以下错误:

Error: Error in :0:0 caused by: Plotly is
ReferenceError: Plotly is not defined

Plotly is an external, chart library. Plotly是一个外部图表库。 I've tried to declare it inside the test file: 我试图在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+/);
  });

});

Unfortunately, it still doesn't see the Plotly and returns error. 不幸的是,它仍然看不到Plotly并返回错误。 The first test case for getScreenHeight() function is not even being started. getScreenHeight()函数的第一个测试用例甚至没有启动。

Maybe jasmine is starting the tests even before it was uploaded? 也许茉莉花甚至在上传之前就开始测试了吗?

Edit : I'm importing Plotly by including <script src='plotly.js'></script> in my index.html file. 编辑 :通过在我的index.html文件中包含<script src='plotly.js'></script>来导入Plotly Plotly is stored locally in the same folder as index.html is. Plotlyindex.html存放在本地相同的文件夹中。

Load plotly.js ( with appropriate path ) file into Karma testing env by adding files config in karma.config.js as below: 负载plotly.js( 具有适当的路径 )通过添加文件如下karma.config.js配置文件到噶测试ENV:

karma.config.js karma.config.js

config.set({
   ..
   files: ['plotly.js']
   ..
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM