Testing functional components with renderIntoDocument

I am learning to test React stateless components using the ReactTestUtils library. This is my simple component:

import React from 'react';

const Greeter = ({name,place}) => (
  <h1>Hello,{name}. Welcome to the {place}.</h1>

export default Greeter;

This is my test spec, to get the renderIntoDocument working, I wrapped my Greeter component in a div as suggested here:

import {expect} from 'chai';
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import Greeter from '../Greeter';

describe('Greeter Components',() => {
  it('renders correctly',() => {
    var component = ReactTestUtils.renderIntoDocument(<div>
        <Greeter name="Vamsi" place="Hotel California"/>

    var hasH1 = ReactTestUtils.findRenderedDOMComponentWithTag(component,'h1');

I get the error

findAllInRenderedTree(...): instance must be a composite component.

I am providing my code as jsbin here.



Since function components don't have an instance associated with them, you can't use them directly with render or renderIntoDocument. Attempting to wrap the function component is a good idea, unfortunately using a div doesn't work for a similar reason. DOM components also don't return a component instance, instead they return the underlying DOM node.

Which is all to say that you can't use the test utils function or native components as the "root" component you are rendering. Instead you will want to wrap your function components in a wrapper component that uses createClass or extends React.Component.

class Wrapper extends React.Component {
  render() { 
    return this.props.children

let component = renderIntoDocument(<Wrapper><Greeter /></wrapper>

Gotcha's like this may be reason enough to make use of a third-party testing library like the popular enzyme, or my own take: teaspoon. Both abstract over issues like this by seamlessly wrapping and unwrapping function components for you, so you don't need to worry about what type of component you are trying to render.


Wrapping functional components in a <div> works for me. You just have to search for the component you want to test a little differently, i.e.

const props = { p1: "1" }
test('Foo renders correctly classed div', () => {
  const cpt = TestUtils.renderIntoDocument(<div><Foo {...props} /></div>);
  const myNode = ReactDOM.findDOMNode(cpt.childNodes[0]);

notice that you can target myNode for testing using cpt.childNodes[0]


In order to improve @monastic-panic's answer, my two cents:

You don't have to create a class for that. Do it dynamically:

import createReactClass from 'create-react-class';

// need to be a class component
const Clazz = createReactClass({
  render: () => {
    return <YourFunctionalComponentName {...props} />;

ReactTestUtils.renderIntoDocument(<Clazz />);


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.