recharts: ResponsiveContainer warning broken in test environment
Do you want to request a feature or report a bug?
Bug report
What is the current behavior?
Even if a proper size is defined in the ResponsiveContainer, the ResponsiveContainer component will log a warning in tests thinking the calculated size is 0 while displaying the right values: Example for minWidth(200) & minHeight(100):
WARN: 'The width(0) and height(0) of chart should be greater than 0,
please check the style of container, or the props width(100%) and height(100%),
or add a minWidth(200) or minHeight(100) or use aspect(undefined) to control the
height and width.'
steps to reproduce:
- clone recharts repo
- run
npm run test - test is passing but still displaying the warning
What is the expected behavior?
No warning when the right value is passed. Even when a minWidth / minHeight / aspect is passed, the calculated width or height is still 0. It renders properly in the browser, this only happens in tests.
Which versions of Recharts, and which browser / OS are affected by this issue? Did this work in previous versions of Recharts?
Tested on 1.0.0-alpha.0, on macOS 10.12.4 running tests with Mocha & Enzyme. Same thing with version 0.22.1.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 31
- Comments: 20 (1 by maintainers)
Commits related to this issue
- feat(component): Condicional remove Change && by ||. Fixes #727 — committed to mogavin/recharts by mogavin 7 years ago
- feat(component): Condicional remove Change && by ||. Fixes #727 — committed to raviteja83/recharts by mogavin 7 years ago
A fast solution would be to define some mock value for the
clientWidthandclientHeighton thebeforeAllThis is what I did in
jest.setup.jsto mitigate the issue for now:same with me, whenever I am using Responsive Container. I am getting same error while unit testing the code
This didn’t do the work for me since both containerWidth & Height remained 0. I used the following mitigation to solve this issue while testing with enzyme:
This if not optimal, but it’s a quick mitigation.
Yep, I am also still encountering this warning on 1.6.2.
it worked like this for me in React 18 and Testing-library/react 13
also receiving this warning with 2.0.0-beta.1
still receiving it as well…
How to get rid of the warning?
I had some trouble with the solutions above. I managed to make it work with the following mock:
If anyone is interested in a Vitest solution
This is not working for me