在组件上初始化UT时,测试代码运行到fixture.deteChanges()时出现错误 组件.ts↓
ngAfterViewInit(): void {
this.initChartOption();
}
public initChartOption(): void {
const chartDom = document.getElementById('line-chart');
const myChart = echarts.init(chartDom);
this.chart = myChart;
myChart.setOption(this.chartOption);
......
}
控制台错误↓
myChart.setOption(this.chartOption);
^
TypeError:Cannot set properties of undefined (setting "dpr")
Jest-canvas-mock 已导入
有没有解决类似问题的经验?请帮帮我,困扰我半个月了😭
我在beforeEach中嘲笑了dom,但是没有作用
const chartDom = document.createElement('div')
chartDom.id = 'line-chart';
document.body.appendChild(chartDom);
我得到答案了
beforeEach(()=>{
jest.spyOn(echarts, 'init').mockImplementation(() => ({
setOption: jest.fn(),
resize: jest.fn(),
dispatchAction: jest.fn()
} as any));
})