Skip to content

Instantly share code, notes, and snippets.

@bvaughn
Last active July 11, 2018 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvaughn/fbf41b3f895bf2d297935faa5525eee9 to your computer and use it in GitHub Desktop.
Save bvaughn/fbf41b3f895bf2d297935faa5525eee9 to your computer and use it in GitHub Desktop.
TestUtils.mockComponent() deprecation

Replacing TestUtils.mockComponent()

TestUtils.mockComponent() has been confusing to React users because it relies on Jest-specific features, and only works when Jest module mocking is enabled. Additionally, it was broken for common patterns like functional components. For these reasons we are removing it from the React TestUtils package.

If you depend on it, you can copy and paste this drop-in replacement into your project:

function mockComponent(component, mockTagName) {
  mockTagName = mockTagName || component.mockTagName || 'div';

  component.prototype.render.mockImplementation(function() {
    return React.createElement(mockTagName, null, this.props.children);
  });

  return this;
}  

It is completely equivalent to what ReactTestUtils.mockComponent() has been doing.

Check out this page for more information about mocking components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment