Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用Jest/Ezyme测试Chart.js-未能创建图表:can';t从给定项获取上下文_Reactjs_Chart.js_Jestjs_Enzyme - Fatal编程技术网

Reactjs 使用Jest/Ezyme测试Chart.js-未能创建图表:can';t从给定项获取上下文

Reactjs 使用Jest/Ezyme测试Chart.js-未能创建图表:can';t从给定项获取上下文,reactjs,chart.js,jestjs,enzyme,Reactjs,Chart.js,Jestjs,Enzyme,我的测试通过得很好,但是我得到了以下错误 console.error node_modules/chart.js/src/core/core.controller.js:127 Failed to create chart: can't acquire context from the given item 我环顾四周,这里提到了我能找到的最接近的答案:但看起来似乎错误是一种故意的方式,在不导致测试失败的情况下优雅地失败 我正在使用Jest/Ezyme编写一个测试,检查哪些选项正在传递

我的测试通过得很好,但是我得到了以下错误

  console.error node_modules/chart.js/src/core/core.controller.js:127
  Failed to create chart: can't acquire context from the given item
我环顾四周,这里提到了我能找到的最接近的答案:但看起来似乎错误是一种故意的方式,在不导致测试失败的情况下优雅地失败

我正在使用Jest/Ezyme编写一个测试,检查哪些选项正在传递给我的图表组件

  it('xAxis set to false', () => {
    const wrapper = mount(<Chart xAxis='false' chart={parentState.chart} />);
    const BarChart = wrapper.find('Bar');

    console.log(BarChart.props().options);
    expect(BarChart.props().options.scales.xAxes[0].display).toEqual(false);

    wrapper.unmount();
  });
it('xAxis设置为false',()=>{
const wrapper=mount();
const BarChart=wrapper.find('Bar');
log(BarChart.props().options);
expect(BarChart.props().options.scales.xAxes[0].display).toEqual(false);
wrapper.unmount();
});

当我在提问之前花一天时间研究我的问题,然后在15分钟后自己找出答案时,我总是觉得自己很傻

我不会删除这篇文章,而是为其他可能有类似问题的人发布答案。我意识到我正在使用chart.js的包装器,
react-chartjs-2
,所以我在他们的github上搜索,确信有人已经发布了我问题的确切答案

将以下模拟添加到我的
setup jest.js
文件解决了我遇到的控制台错误

jest.mock('react-chartjs-2', () => ({
  Bar: () => null
}));

如何模拟折线图选项中的函数。根据我的要求,我编写了自定义函数并将其附加到onHover和animation onProgress。如何模拟这些函数?到目前为止,我的代码覆盖率很低。请提供帮助。@TestMail我不确定,我建议用
chart.js