繁体   English   中英

运行笑话测试进行反应时出现错误

[英]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()
  })
})

我认为嘲弄与未定义错误的addStyleRule有关,但是对于使用外部库的这种特殊情况,我还没有明确确定需要嘲弄/嘲笑什么

我最终只是取消了对密闭库的模拟。 警告:由于它实际上正在渲染,因此它会使测试运行极其缓慢。

奖励:覆盖组件道具

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM