I have this component, which uses react-chartjs-2 to render a Doughnut chart:
const CompliancyChart = ({data}): JSX.Element => {
...
return (
<ChartStyled>
{chartPlugins && chartData && (
<Doughnut
aria-label="Compliancy Chart"
data={chartData}
options={chartOptions}
plugins={chartPlugins}
/>
)}
</ChartStyled>
);
};
And I have a test file for this component which has this assertion:
describe('Chart component', () => {
afterEach(() => {
cleanup();
});
test('should render without errors', async () => {
render(<CompliancyChart data={mockCompliancyChartData} />, {});
const compliancyChart = await screen.findByRole('img', {
name: 'Compliancy Chart',
});
expect(compliancyChart).toBeDefined();
});
});
This test is working fine, but I keep getting this error in the test console:
Failed to create chart: can't acquire context from the given item
The only solution I could find online is to use:
jest.mock('react-chartjs-2', () => ({
Doughnut: () => null,
}))
Which in this case my test fails, since I can't test against canvas inside the screen container: (https://github.com/reactchartjs/react-chartjs-2/issues/155#issuecomment-821322545)
How can I solve this issue?