运行jest test for react

时间:2016-08-01 19:21:25

标签: javascript reactjs plotly jestjs

  - 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)

我的反应项目使用了情节,并且在运行一次开玩笑测试时遇到了麻烦。

我的测试代码如下:

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()
  })
})

我认为嘲弄情节与未定义错误的addStyleRule有关但我还没有确定需要为使用外部库的特定情况嘲笑/取消哪些内容

1 个答案:

答案 0 :(得分:0)

我最后只是嘲笑这个阴谋图书馆。警告它会使您的测试运行速度极慢,因为它现在实际上正在渲染。

奖励:覆盖组件道具

  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}/>
  });