Error when testing with jest
I'm using jest and enzyme to write tests for my project. When testing components with charts in them and calling component.unmount(), I'm getting this error:
TypeError: Cannot read property 'destroy' of undefined
at ChartComponent.componentWillUnmount (node_modules/react-chartjs-2/lib/index.js:127:26)
at node_modules/react-dom/lib/ReactCompositeComponent.js:409:25
...pointlessly looking enormous trace
Full stack trace here: http://pastebin.com/yHNX5QnV
Does anybody else have any similar problem, do I have to do something more specific when testing components with charts in them and do you have any idea how I might be able to fix this?
+1
I am getting the same error when using react-chartjs-2 in a react webapp. Is there a solution to this issue?
Can you put together a quick sample of what you're doing so I can see? It seems like the chart instance is not initialized when you're destroying it.
I have been having a similar issue: TypeError: Cannot read property 'length' of null
I've created a small repo to reproduce it.
Any suggestions?
Link to my branch: https://github.com/armenzg/react-chartjs-2-test/tree/testCase
STR:
git clone [email protected]:armenzg/react-chartjs-2-test.git
cd react-chartjs-2-test
git checkout testCase
yarn install
yarn test
Output:
armenzg@armenzg-mbp react-chartjs-2-test$ yarn test
yarn run v1.10.1
$ neutrino test
FAIL test/ChartJsWrapper.test.jsx
● Console
console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6884
The above error occurred in the <ChartComponent> component:
in ChartComponent (created by ChartJsWrapper)
in ChartJsWrapper
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6884
The above error occurred in the <ChartComponent> component:
in ChartComponent (created by ChartJsWrapper)
in ChartJsWrapper
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
● renders correctly
TypeError: Cannot read property 'length' of null
at Object.acquireContext (node_modules/chart.js/src/platforms/platform.dom.js:340:19)
at Chart.construct (node_modules/chart.js/src/core/core.controller.js:83:27)
at new Chart (node_modules/chart.js/src/core/core.js:42:8)
at ChartComponent.renderChart (node_modules/react-chartjs-2/lib/index.js:259:26)
at ChartComponent.componentDidMount (node_modules/react-chartjs-2/lib/index.js:80:10)
at commitLifeCycles (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7019:22)
at commitAllLifeCycles (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:8234:7)
at HTMLUnknownElement.callCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2312:14)
at Object.invokeGuardedCallbackDev (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2362:16)
at invokeGuardedCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2411:31)
at commitRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:8404:7)
at completeRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9769:3)
at performWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9697:9)
at performWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9601:7)
at performSyncWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9573:3)
at requestWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9461:5)
at scheduleWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9256:5)
at scheduleRootUpdate (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9865:3)
at updateContainerAtExpirationTime (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9892:10)
at updateContainer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9903:10)
at Object.create (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10428:5)
at Object.<anonymous>.it (test/ChartJsWrapper.test.jsx:8:3)
at new Promise (<anonymous>)
at process._tickCallback (internal/process/next_tick.js:68:7)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.854s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
+1
Closing for now. Please supply a sandbox or code example with the latest versions of both libraries. If the problem persists, feel free to open a new issue.