[Chartjs]-Testing Chart.js with Jest/Enzyme – Failed to create chart: can't acquire context from the given item

23πŸ‘

βœ…

I always feel pretty silly when I spend a day researching my question prior to asking it, then I figure it out on my own 15 minutes later…

Rather than deleting this I’m going to post the answer for anyone else that may have a similar issue. I realized that I’m using a wrapper for chart.js, react-chartjs-2 so I searched on their github and sure enough someone had already posted the exact answer to my question. https://github.com/jerairrest/react-chartjs-2/issues/155

Adding the following mock to my setup-jest.js file resolved the console errors I was getting.

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

Leave a comment