运行 开玩笑测试反应时出现错误
Plotly error when running jest test for react
- 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)
我的 React 项目使用 plotly,当 运行 开玩笑测试
时 plotly 有问题
我的测试代码是这样的:
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()
})
})
我认为 mocking plotly 与 addStyleRule of undefined error 有关,但我还没有完全确定需要什么 mocked/unmocked 对于这种使用外部库的特殊情况
我最终只是取消了 plotly 库的模拟。警告它使您的测试 运行 非常慢,因为它现在实际上正在渲染。
奖励:覆盖组件 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}/>
});
- 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)
我的 React 项目使用 plotly,当 运行 开玩笑测试
时 plotly 有问题我的测试代码是这样的:
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()
})
})
我认为 mocking plotly 与 addStyleRule of undefined error 有关,但我还没有完全确定需要什么 mocked/unmocked 对于这种使用外部库的特殊情况
我最终只是取消了 plotly 库的模拟。警告它使您的测试 运行 非常慢,因为它现在实际上正在渲染。
奖励:覆盖组件 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}/>
});