【问题标题】:react testing: shallow rendering - getRenderOutput returning null反应测试:浅渲染 - getRenderOutput 返回 null
【发布时间】:2015-11-23 11:44:21
【问题描述】:

我刚刚开始进行反应测试,尝试使用浅层渲染。

我不知道这里发生了什么 - 我相信我的代码遵循了我在示例中看到的所有说明(例如 http://racingtadpole.com/blog/test-react-with-jest/

但我使用shallowRenderer.getRenderOutput(); 得到空值

另外我很困惑,当我退出 shallowRenderer 时,我没有看到任何 getRenderOutput 方法??

import React from 'react/addons';
import Icon from '../../components/shared/Icon.jsx';

const TestUtils = React.addons.TestUtils;

function getRenderOutput(elt) {
  console.log('elt: ', elt);
  const shallowRenderer = TestUtils.createRenderer();
  shallowRenderer.render(elt);
  console.log('shallowRenderer: ', shallowRenderer);
  const output = shallowRenderer.getRenderOutput();
  console.log('output: ', output);
  return output;
}


describe('<Icon />', () => {

  it('should pass props through', () => {
    const result = getRenderOutput(<Icon type='octicon' name='file-directory' classNames="icon-roomy icon-muted" />);
    console.log('result: ', result);
  });

});

结果输出:

elt:  

{ type:
   { [Function: Icon]
     _isMockFunction: true,
     mock: { calls: [], instances: [] },
     mockClear: [Function],
     mockReturnValueOnce: [Function],
     mockReturnValue: [Function],
     mockImpl: [Function],
     mockImplementation: [Function],
     mockReturnThis: [Function],
     _getMockImplementation: [Function],
     defaultProps: { type: 'fa', classNames: '' },
     propTypes:
      { type: [Object],
        name: [Object],
        prefix: [Object],
        classNames: [Object],
        title: [Object] } },
  key: null,
  ref: null,
  _owner: null,
  _context: {},
  _store:
   { props:
      { type: 'octicon',
        name: 'file-directory',
        classNames: 'icon-roomy icon-muted' },
     originalProps:
      { type: 'octicon',
        name: 'file-directory',
        classNames: 'icon-roomy icon-muted' } } }
shallowRenderer:  { _instance:
   { _currentElement:
      { type: [Object],
        key: null,
        ref: null,
        _owner: null,
        _context: {},
        _store: [Object] },
     _rootNodeID: '.0',
     _instance:
      { getClassNames: [Object],
        render: [Object],
        setState: [Object],
        forceUpdate: [Object],
        props: [Object],
        context: {},
        refs: {},
        _reactInternalInstance: [Circular],
        state: null },
     _pendingElement: null,
     _pendingStateQueue: null,
     _pendingReplaceState: false,
     _pendingForceUpdate: false,
     _renderedComponent: { _renderedOutput: null, _currentElement: [Object] },
     _context: {},
     _mountOrder: 1,
     _isTopLevel: false,
     _pendingCallbacks: null } }
output:  null
result:  null

【问题讨论】:

    标签: reactjs reactjs-testutils


    【解决方案1】:

    我是个白痴。我只是没有模拟我的组件

    jest.dontMock('../../components/shared/Icon.jsx');
    

    【讨论】:

      猜你喜欢
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2019-03-14
      • 2020-03-12
      • 2020-03-12
      • 2016-09-03
      相关资源
      最近更新 更多