Solution | TypeError: parentInstance.children.indexOf is not a function – React Testing Library

Error:
TypeError: parentInstance.children.indexOf is not a function
This error shows up while using create method of the React Testing Library for snapshot matching.

Versions: React version 16

Cause: ReactDOM.createPortal is not supported inside a React TestRenderer tree. Another renderer can’t be used in addition to the test renderer.
parentInstance.children in case of portal is an instance of HTMLCollection, but test-renderer expects array of Instance type or TextInstance type in appendChild method.

Solution: As a work around use toJson method from enzyme-to-json to match the snapshot
import toJson from 'enzyme-to-json';

test('should match the snapshot', () => {
expect(toJson(yourComponentRenderedMethod())).toMatchSnapshot()
})

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.