angular - Angular 6 导入 Karma 测试中使用的 3rd 方 JS 文件的正确方法
问题描述
我有一个使用 primeng 的组件,ChartsModule
它引用Chart.js
. 我已经Chart.js
安装并将它导入到我的angular.json
文件中。
"scripts": [
"node_modules/chart.js/dist/Chart.bundle.min.js"
]
该应用程序一切正常并显示图表。但是,我的组件测试因错误而中断:ReferenceError: Chart is not defined
我看过一些文章建议将 Chart.js 导入到我的组件测试中,如下所示:
import { Chart } from 'chart.js';
这行不通。我的问题是将 3rd 方 JS 库(例如 Chart.js)导入 Angular 6 Karma 测试的正确方法是什么
编辑 我的组件
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-my-chart',
templateUrl: './my-chart.component.html',
styleUrls: ['./my-chartcomponent.scss']
})
export class MyChartComponent implements OnInit {
public data: any;
constructor() { }
ngOnInit() {
this.data = {
labels: ['Low', 'Medium', 'High'],
datasets: [
{
data: [300, 50, 10],
backgroundColor: [
'#43a047',
'#fb8c00',
'#e53935'
],
hoverBackgroundColor: [
'#66bb6a',
'#ffa726',
'#ef5350'
]
}]
};
}
}
解决方案
还在用旧angular-cli.json
模式思考。在新angular.json
版本中有一个测试部分,您还必须在其中添加第 3 方脚本。
"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",
"styles": [
"styles.scss"
],
"scripts": [
"node_modules/chart.js/dist/Chart.bundle.min.js"
],
"assets": [
"src/favicon.ico"
]
}
},
推荐阅读
- statistics - 测试和训练分布之间的哪个统计测试
- python-3.x - Selenium 无法在电子邮件模板中找到按钮
- javascript - react中的嵌套路由
- python - 如何将字典存储在文件中(Python)
- ios - 多人连接自动连接
- java - 在 Android java 应用程序中使用服务帐户连接到 google Drive API v3
- go - 在链式方法调用中使用`defer`会发生什么?
- python - 如何在python中切换三个变量的值?
- java - 为什么即使凭据正确,我也会获得 401 未授权?
- python - 如何从 flask-mqtt 回调中获取数据到连续的烧瓶文本/事件流响应中?