question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ResponsiveContainer cannot be rendered and hence cannot be tested using react testing library

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. Use ResponsiveContainer to render a chart
  2. Render the chart component with react testing library

What is expected?

The chart should get rendered

What is actually happening?

<div class="recharts-responsive-container makeStyles-chart-21" style="width: 100%; height: 300px;">
    <div style="position: absolute; width: 0px; height: 0px; visibility: hidden; display: none;"/>
</div>

The chart is hidden with 0 height and width. No dom nodes are created for the chart.

Environment Info
Recharts v1.8.5
React 16.13.0
System macOS Mojave 10.14.6
Browser React testing library

The chart is rendered while testing if ResponsiveContainer is replaced with a div.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

17reactions
joshua-phillipscommented, May 4, 2021

You’re a life-saver, @krzysu! This was making me crazy. The only thing I’ll add is that I had to return all the other recharts components as-is with spread syntax on the original module.

jest.mock('recharts', () => {
    const OriginalModule = jest.requireActual('recharts');

    return {
        ...OriginalModule,
        ResponsiveContainer: ({ height, children }) => (
            <OriginalModule.ResponsiveContainer width={800} height={height}>
                {children}
            </OriginalModule.ResponsiveContainer>
        ),
    };
});

Thanks again!

7reactions
krzysucommented, Apr 14, 2021

you mock ResponsiveContainer in your test, something like below:

jest.mock('recharts', () => {
  const OriginalResponsiveContainerModule = jest.requireActual('recharts');

  return {
    ResponsiveContainer: ({ height, children }) => (
      <OriginalResponsiveContainerModule.ResponsiveContainer
        width={800}
        height={height}
      >
        {children}
      </OriginalResponsiveContainerModule.ResponsiveContainer>
    ),
  };
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing unit tests with react testing library for recharts
Two things you can try: mock the responsiveContainer with at least one fixed width/height value like so;. jest.mock("recharts" ...
Read more >
[React] Writing tests with Charts | by J.Kim - Medium
ResponsiveContainer cannot be rendered and hence cannot be tested using react testing library ·… I have searched the issues of this repository and...
Read more >
API | Testing Library
React Testing Library re-exports everything from DOM Testing Library as well.
Read more >
react.development.js:1622 uncaught typeerror: cannot read ...
getting error "Uncaught (in promise) TypeError: Cannot read properties of null (reading ... "git+https://github.com/anmdsystems/test-component-library.git".
Read more >
Test Utilities - React
ReactTestUtils makes it easy to test React components in the testing framework of your choice. At Facebook we use Jest for painless JavaScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found