I set up an app using create-react-app, which uses jest as a test runner. I am using echarts, installed via npm, imported and use like this:
import echarts from 'echarts/lib/echarts';
//...
const el = document.getElementById('the-chart');
this.chart = echarts.init(el);
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
meaning that echarts is null
In my package.json, echarts is listed under "dependencies", which, as far as I know, means it should be available in dev mode as well.
Why would the value of echarts be null only when running tests? What would differ between the test (jest) and browser environment that could lead to this error? How can I make this component pass its unit test without wrapping the echarts usage in a bunch of hacky guards?
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!
Aucun commentaire:
Enregistrer un commentaire