[英]How to use external js lib into angular component test
I have a component that uses an external js library: leader-line.我有一个使用外部 js 库的组件:leader-line。 When I try to test that component, it always throws an error that function for the external library is not defined.
当我尝试测试该组件时,它总是抛出一个错误,即未定义外部库的函数。
component file
declare var LeaderLine: any;
@Component({
selector: 'app-flow-line',
templateUrl: './flow-line.component.html',
styleUrls: ['./flow-line.component.css']
})
export class FlowLineComponent implements AfterViewInit, OnDestroy {
@Input() public flowPathConfig: any = new Array();
public myLines: any = new Array();
public ngAfterViewInit() {
for (let config of this.flowPathConfig) {
if (document.getElementById(config.fromStep) && document.getElementById(config.toStep)) {
this.myLines.push(new LeaderLine(
document.getElementById(config.fromStep),
document.getElementById(config.toStep)
));
}
}
}
spec file规范文件
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FlowLineComponent } from './flow-line.component';
import { FundamentalNgxModule } from 'fundamental-ngx';
describe('FlowLineComponent', () => {
let component: FlowLineComponent;
let fixture: ComponentFixture<FlowLineComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FlowLineComponent],
imports: [FundamentalNgxModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FlowLineComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('it should draw line with pathConfig', () => {
component.flowPathConfig = [{ fromStep: '1', toStep: '2' }];
let el1 = document.createElement('div');
el1.setAttribute('id', '1');
let el2 = document.createElement('div');
el2.setAttribute('id', '2');
document.body.appendChild(el1);
document.body.appendChild(el2);
fixture.detectChanges();
component.ngAfterViewInit();
expect(component.myLines.length).toEqual(0);
expect(component).toBeTruthy();
});
});
Following is the error trace.以下是错误跟踪。 That shows reference error.
那显示参考错误。 PS: I have included the leader-line library using the following post: Use external javaScript library in angular application
PS:我使用以下帖子包含了领导线库: 在角度应用程序中使用外部 javaScript 库
HeadlessChrome 75.0.3770 (Mac OS X 10.14.6) FlowLineComponent it should draw line with pathConfig FAILED
ReferenceError: LeaderLine is not defined
at <Jasmine>
at FlowLineComponent.LeaderLine [as ngAfterViewInit] (http://localhost:9876/_karma_webpack_/webpack:/src/app/flow/flow-line/flow-line.component.ts:34:43)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/flow/flow-line/flow-line.component.spec.ts:45:15)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:390:1)
at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:150:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:576:1)
at <Jasmine>
HeadlessChrome 75.0.3770 (Mac OS X 10.14.6): Executed 13 of 16 (1 FAILED) (0 secs / 3.274 secs)
HeadlessChrome 75.0.3770 (Mac OS X 10.14.6) FlowLineComponent it should draw line with pathConfig FAILED
ReferenceError: LeaderLine is not defined
at <Jasmine>
at FlowLineComponent.LeaderLine [as ngAfterViewInit] (http://localhost:9876/_karma_webpack_/webpack:/src/app/flow/flow-line/flow-line.component.ts:34:43)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/flow/flow-line/flow-line.component.spec.ts:45:15)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:390:1)
at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:150:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:576:1)
I would try to import the lib like this :我会尝试像这样导入 lib:
import * as LeaderLine from 'leader-line';
instead of the var declaration.而不是 var 声明。 Is it only the tests that doesn't work or when you run your app it throws the error as well ?
只是测试不起作用还是在您运行应用程序时也会引发错误?
I inject the methods in the component by using the function declaration way.我使用函数声明的方式在组件中注入方法。
declare function A();
A way to test only the component's methods, and ignore all the imported ones from the external js file.一种只测试组件方法的方法,忽略所有从外部 js 文件中导入的方法。 You can create a mock/<external.js> file, that will replace the origin on testing mode.
您可以创建一个模拟/<external.js> 文件,它将在测试模式下替换原点。 You have to add it also into the angular.json file in test configuration.
您还必须将它添加到测试配置中的 angular.json 文件中。
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"sourceMap": true,
"assets": [
"src/favicon.ico",
"src/assets",
"src/env.js"
],
"styles": [
"src/styles.css",
"src/sass/intracom-light.scss"
],
"scripts": [
"src/assets/mock/js/maps.js"
]
}
},
Replace the body of the calling methods to set up the testing behavior.替换调用方法的主体以设置测试行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.