首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将外部库包含到单元测试中::Angular2-CLI

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

Stack Overflow用户
提问于 2017-02-13 13:49:44
回答 1查看 3.9K关注 0票数 3

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

代码语言:javascript
复制
Error: Error in :0:0 caused by: Plotly is
ReferenceError: Plotly is not defined

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

代码语言:javascript
复制
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+/);
  });

});

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

也许茉莉花在上传之前就已经开始测试了?

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

EN

回答 1

Stack Overflow用户

发布于 2018-05-18 14:54:24

plotly.js (带有适当路径)文件加载到Karma测试env中,在karma.config.js中添加文件配置,如下所示:

karma.config.js

代码语言:javascript
复制
config.set({
   ..
   files: ['plotly.js']
   ..
});
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42205641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档