- TypeError: Cannot read property 'addStyleRule' of undefined
at Object.<anonymous> (node_modules/plotly.js/build/plotcss.js:61:15)
at Object.<anonymous> (node_modules/plotly.js/src/plotly.js:30:1)
at Object.<anonymous> (node_modules/plotly.js/src/core.js:15:14)
at Object.<anonymous> (node_modules/plotly.js/lib/core.js:9:18)
at Object.<anonymous> (node_modules/plotly.js/lib/index.js:15:12)
at emitTwo (events.js:106:13)
at process.emit (events.js:191:7)
at process.nextTick (internal/child_process.js:719:12)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
My react project uses plotly and it is having trouble with plotly when running a jest test
My test code looks like such:
import React from 'react'
import ReactDOM from 'react-dom'
import TestUtils from 'react-addons-test-utils'
import AppBar from "./index"
import Navigation from "../navigation/"
// Use real modules for testing.
jest.unmock("./index")
jest.unmock("./brand")
describe("AppBar", () => {
let component = <AppBar />
let element = TestUtils.renderIntoDocument(component)
let node = ReactDOM.findDOMNode(element)
it("renders the navigation to screen", () => {
let el = TestUtils.findRenderedComponentWithType(element, Navigation)
expect(el).toBeDefined()
})
})
I figure that mocking plotly has something to do with the addStyleRule of undefined error but I haven't quite pinned down as to what needs to be mocked/unmocked for this particular case of using an external library
I ended up just unmocking the plotly library. WARNING it makes your test run extremely slow since it is now actually rendering.
Bonus: overriding component props
import Plotly from 'plotly.js'
import MyComponent from "./index";
// Use real modules for testing.
jest.unmock("plotly.js");
jest.unmock("./index");
describe("My component does something", () => {
let params = {
display: true
};
let ComponentMock = class extends MyComponent {
constructor(props) {
super(props);
this.actions = {
myAction: null
}
}
};
let el = <ComponentMock params={params}/>
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.