gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:34:01 24 4
gpt4 key购买 nike

我正在为我的项目编写测试用例。但不幸的是,在输入 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() 的第一个测试用例功能甚至没有被启动。

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

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

最佳答案

通过在 karma.config.js 中添加文件配置,将 plotly.js(具有适当的路径)文件加载到 Karma 测试环境中,如下所示:

karma.config.js

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

关于javascript - 如何将外部库包含到单元测试中::Angular2-CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42205641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com