[EDITED]: an earlier version of this question mistakenly though the echarts variable was null, when it was the result of getElementById. I confirmed this by remove el from the function call, and that part of the test did not generate a similar error, implying that echarts was in fact available.
I am using create-react-app, which uses the jest test runner, and am using echarts.
import echarts from 'echarts/lib/echarts';
//...
const el = document.getElementById('the-chart');
this.chart = echarts.init(el);
//...
// lots of unecessary context ...'d out here:
render() {
const { classes } = this.props;
return (
<div className={...}>
<div className={...}><!-- ... --></div>
<div className={classes.metricsGraph} onClick={(e) => this.handleClick(e)}>
<div id="the-chart" style={{width: "600px",height: "400px"}}></div>
</div>
<div className={...}><!-- ... --></div>
</div>
);
}
This works when I load the page in a browser. However, when I run tests, I get, on this last line,
TypeError: Cannot read property 'getAttribute' of null
The value of el, returned by document.getElementById must be returning null, but only in the test environment. This code is being called in the components componentDidMount callback.
How can I proceed to make this test pass? Is there another way to get the element or another event I should wait for in the test environment to have this element accessible? Where can I look to discover why this is different in test vs browser?
I am relatively new to node / npm, jest, Javascript that sin't from the 90's, and React, so any pointers to things I'm not thinking about correctly would be greatly appreciated!