[英]How to include external library into unit tests::Angular2-CLI
我正在為我的項目編寫測試用例。 但是不幸的是,在通過鍵入ng test
開始測試之后,我收到以下錯誤:
Error: Error in :0:0 caused by: Plotly is
ReferenceError: Plotly is not defined
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+/);
});
});
不幸的是,它仍然看不到Plotly
並返回錯誤。 getScreenHeight()
函數的第一個測試用例甚至沒有啟動。
也許茉莉花甚至在上傳之前就開始測試了嗎?
編輯 :通過在我的index.html
文件中包含<script src='plotly.js'></script>
來導入Plotly
。 Plotly
與index.html
存放在本地相同的文件夾中。
負載plotly.js( 具有適當的路徑 )通過添加文件如下karma.config.js配置文件到噶測試ENV:
karma.config.js
config.set({
..
files: ['plotly.js']
..
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.