question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

0.14-rc1: findDOMNode(statelessComponent) doesn’t work with TestUtils.renderIntoDocument

See original GitHub issue

Unsure if this also impacts in a browser. I wrote an example of this here: https://github.com/iamdustan/react-testing-stateless-components

I wrote two components. One with class Component extends React.Component and the other var Component = (props) => ().

The test case is identical for each. The class component test passes. The function component test fails.

_https://github.com/iamdustan/react-testing-stateless-components/blob/master/class.js_

import React from 'react';

class Component extends React.Component {
  render() {
    var {onClick, text} = this.props;
    return (
      <button onClick={onClick}>{text}</button>
    );
  }
};

Component.propTypes = {
  onClick: React.PropTypes.func.isRequired,
  text: React.PropTypes.string.isRequired,
};

export default Component;

_https://github.com/iamdustan/react-testing-stateless-components/blob/master/stateless.js_

import React from 'react';

var Component = ({onClick, text}) => (
  <button onClick={onClick}>{text}</button>
);

Component.propTypes = {
  onClick: React.PropTypes.func.isRequired,
  text: React.PropTypes.string.isRequired,
};

export default Component;

Output

Using Jest CLI v5.0.3
 FAIL  __tests__/stateless-test.js (0.998s)
● thing › it render
  - TypeError: Cannot read property 'textContent' of null
        at Spec.<anonymous> (/Users/dkasten/projects/react-test-case/__tests__/stateless-test.js:15:34)
        at jasmine.Block.execute (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
        at jasmine.Queue.next_ (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
        at null._onTimeout (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
        at Timer.listOnTimeout (timers.js:89:15)
 PASS  __tests__/class-test.js (1.109s)
1 test failed, 1 test passed (2 total)

This failure is not due to interactions with Jest as it also exists on another project that is not using Jest (although still using jsdom).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
dinhodoescommented, Mar 31, 2016

One way to avoid ‘null’ or the TestUtils altogether when testing event handlers is to instantiate the component as a constructor (i.e. var comp = new MyComp(props) ).

Interesting find from this article.

1reaction
iamdustancommented, Sep 10, 2015

I moved the stateless currently failing approach to wrap it in a <div /> with the test utils. From this I can get to testing what I want, but it’s a bit awkward.

var component = TestUtils.renderIntoDocument(<div><StatelessComponent ... /></div>);
var statelessComponent = findDOMNode(component).children[0];

https://github.com/iamdustan/react-testing-stateless-components/blob/master/__tests__/stateless-failure-test.js https://github.com/iamdustan/react-testing-stateless-components/blob/master/__tests__/stateless-success-test.js

Is there a better way of doing this that I’m unaware of? Or is this just the way it is going to be for a while?

Read more comments on GitHub >

github_iconTop Results From Across the Web

附录三、React 测试入门教学
以下是 react-addons-test-utils-example/src/test/shallowRender.test.js : ... 0.14-rc1: findDOMNode(statelessComponent) doesn't work with TestUtils.
Read more >
附录三、React 测试入门教学- 《从零开始学ReactJS(ReactJS 101 ...
以下是 react-addons-test-utils-example/src/test/shallowRender.test.js : 复制代码 ... 0.14-rc1: findDOMNode(statelessComponent) doesn't work with TestUtils.
Read more >
React讀書筆記Script - 程式人生
0.14-rc1 : findDOMNode(statelessComponent) doesn't work with TestUtils.renderIntoDocument #4839. Writing Redux Tests 【譯】展望2016,React.js ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found