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.

TypeError: Cannot read property 'parentElement' of null

See original GitHub issue

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

I’m trying to use the ResponsiveContainer to wrap a component but in tests (Jest), i’m getting the following error:

TypeError: Cannot read property 'parentElement' of null

Here’s a gist with the test and the component:

https://gist.github.com/Carlows/e276e740a2a739d3a73cb1053d386134

The same error happened to me when trying to render the LineChart component in a test.

Which versions of Recharts, and which browser / OS are affected by this issue? Did this work in previous versions of Recharts?

Version 0.22.0, I’m not aware of this working or not working in other versions, but I didn’t found anything related to this anywhere.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

25reactions
hendrikmoldercommented, Jul 18, 2017

The issue comes from the ResponsiveContainer component and is caused by the use of refs. As a workaround you can mock refs in snapshot testing

The following test should work

import React from 'react';
import renderer from 'react-test-renderer';
import { BarChart } from '../';

function createNodeMock() {
  const doc = document.implementation.createHTMLDocument();
  return { parentElement: doc.body };
}

describe('BarChart', () => {
  test('renders properly', () => {
    const data = [
      { name: 'A-1', value: 0 },
      { name: 'A-2', value: 10 },
      { name: 'A-3', value: 80 },
      { name: 'A-4', value: 60 },
      { name: 'A-5', value: 45 },
    ];

    const tree = renderer.create(
      <BarChart data={data} color="#ff6363" />,
      { createNodeMock }
    ).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
8reactions
usulprocommented, Jun 10, 2019

Hey @xile611 this issue is fixed in react-resize-detector (see https://github.com/maslianok/react-resize-detector/pull/40) But this package is still depends on an old version https://github.com/recharts/recharts/blob/13997461cd7f82302704c14a20fb123b2433338c/package.json#L64 Don’t you consider to update the version of this dependency to the latest?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript Cannot read property 'parentElement' of null
Using Chome's debug mode (F12), set a breakpoint on the line oldImage.parentElement. · It could be that the script is running before the...
Read more >
waitForElementToBeRemoved: TypeError: Cannot read ...
Problem description: react-modal is removing the parentElement of the queried element. The queried element is given to waitForElementToBeRemoved ...
Read more >
TypeError: Cannot read property 'parentNode' of Null in JS
The "Cannot read property 'parentNode' of null" error occurs when you access the parentNode property on a null value. To solve the error,...
Read more >
TypeError: Cannot read property 'parentElement' of null
Fresh project generated by create-react-app@4: @testing-library/dom version: 5.11.4; Testing Framework and version: <!-- are you using jest, ...
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
All this means is that you are trying to access a property of an object that is undefined. These usually happens when we...
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