在Angular中对Echarts(setOption)进行jest单元测试时出错

9w11ddsr  于 11个月前  发布在  Jest
关注(0)|答案(1)|浏览(181)

在组件上初始化UT时,测试代码运行到**fixture.deteChanges()**component.ts时出错↓

ngAfterViewInit(): void {
     this.initChartOption();
}

个字符
控制台错误↓

myChart.setOption(this.chartOption);
        ^
TypeError:Cannot set properties of undefined (setting "dpr")


Jest-canvas-mock已导入
你有解决类似问题的经验吗?请帮助我,因为它已经困扰了我半个月了。
我以前嘲笑过dom,但没有用

const chartDom = document.createElement('div')
chartDom.id = 'line-chart';
document.body.appendChild(chartDom);

w46czmvw

w46czmvw1#

我得到了答案

beforeEach(()=>{
    jest.spyOn(echarts, 'init').mockImplementation(() => ({
        setOption: jest.fn(),
        resize: jest.fn(),
        dispatchAction: jest.fn()
    } as any));
})

字符串

相关问题