I am facing an issue with my Angular component that uses primeng's ChartsModule
which relies on Chart.js
. I have successfully installed and imported Chart.js
in my project's angular.json
file.
"scripts": [
"node_modules/chart.js/dist/Chart.bundle.min.js"
]
While the application functions correctly and displays the charts, the component tests are failing with the error:
ReferenceError: Chart is not defined
I have come across suggestions to import Chart.js into the component tests like this:
import { Chart } from 'chart.js';
However, this approach does not work. So, I'm curious about the proper way to import third-party JS libraries such as Chart.js into Angular 6 Karma tests.
Edit Here is a snippet of my component:
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'
]
}]
};
}
}