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.'

The calculatedWidth / calculatedHeight in ResponsiveContainer don’t take the minWidth & minHeight into account.

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

Most upvoted comments

A fast solution would be to define some mock value for the clientWidth and clientHeight on the beforeAll

beforeAll(() => {
  jest.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(100);
  jest.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(100);
});

This is what I did in jest.setup.js to mitigate the issue for now:

const MockResponsiveContainer = props => <div {...props} />

jest.mock('recharts', () => ({
  ...jest.requireActual('recharts'),
  ResponsiveContainer: MockResponsiveContainer,
}))

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:

    let containerProps = {};
    if ('a case where I know this will happen') {
      containerProps['width'] = 100; // some default values
      containerProps['aspect'] = 1;
    }

    return (
      <ResponsiveContainer {...containerProps}>
        {children}
      </ResponsiveContainer>
    );
  }

This if not optimal, but it’s a quick mitigation.

Yep, I am also still encountering this warning on 1.6.2.

This is what I did in jest.setup.js to mitigate the issue for now:

const MockResponsiveContainer = props => <div {...props} />

jest.mock('recharts', () => ({
  ...jest.requireActual('recharts'),
  ResponsiveContainer: MockResponsiveContainer,
}))

it worked like this for me in React 18 and Testing-library/react 13

jest.mock('recharts', () => ({
  ...jest.requireActual('recharts'),
  ResponsiveContainer: props => <div {...props} />,
}))

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:

// /__mocks__/recharts.js

import { Children, cloneElement } from "react";

export function ResponsiveContainer(props) {
  return (
    <div>
      {Children.map(props.children, (child) =>
        cloneElement(child, { width: 100, height: 100 })
      )}
    </div>
  );
}

export * from "recharts";

If anyone is interested in a Vitest solution

  vi.mock('recharts', async (importOriginal) => {
    const originalModule = (await importOriginal()) as Record<string, unknown>
    return {
      ...originalModule,
      ResponsiveContainer: () => createElement('div'),
    }
  })

A fast solution would be to define some mock value for the clientWidth and clientHeight on the beforeAll

beforeAll(() => {
  jest.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(100);
  jest.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(100);
});

This is not working for me