【问题标题】:React with jest renderIntoDocument always returns null用开玩笑的 renderIntoDocument 反应总是返回 null
【发布时间】:2016-08-10 14:09:36
【问题描述】:

我一直在尝试使用 jest 测试一个反应应用程序。但甚至不能更进一步。我创建了一个简单组件,并尝试用玩笑对其进行测试。

这里是组件

import React from 'react'

export default class TestComponent extends React.Component{
 render(){
    return(
        <h1>This is a Test Component</h1>
    )
 }
}

和测试

jest.dontMock('../src/components/TestComponent')

import TestComponent from '../src/components/TestComponent'
var React = require('react'),
TestUtils = require('react-addons-test-utils')

describe('TestComponent', () =>{

it('should Render Somehow',() => {
    let testComponent = TestUtils.renderIntoDocument(
        <TestComponent />
    )
    let com = TestUtils.scryRenderedDOMComponentsWithTag(testComponent, 'h1')
      expect(com.length).toEqual(1);
    console.log(com)
})
})

testComponent 的输出并不是真正的 null,它会打印出以 'Testcomponent{' 开头的内容,但是当我尝试使用 scryRenderedDOMComponentsWithTag 方法从 testComponent 中找出任何内容时,这始终是 null。

谢谢

【问题讨论】:

    标签: reactjs jestjs


    【解决方案1】:

    解决办法是改变

    jest.dontMock('../src/components/TestComponent')
    

    jest.unmock('../src/components/TestComponent')
    

    link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2014-09-18
      • 2021-09-27
      • 2021-05-22
      • 2020-10-24
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多