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.

Better test facilities for stateless components

See original GitHub issue

I originally left a comment in #4936, but I felt this might be better as a separate issue.

I completely agree with the rationale behind no backing instances and no refs for stateless components. I want to use pure stateless components as much as possible.

However, I’m running into instances where testing stateless components gets tricky. For the most part, using the shallow renderer to render a stateless component has been fine. I can then make assertions on expected props and children. To reduce the duplication, I abstract out the shallow renderer process to a helper function.

When, I want to test events, though, I have to take a different route. If my component takes an onClick prop, then I wrap it in a class component, so I can access the DOM node to simulate my click event. Again, this isn’t a huge deal because I can abstract out the wrap/render process into a helper function.

I bring all this up because I feel that it would be nice to have better facilities in TestUtils for stateless components. I don’t think the answer is allowing ReactDOM.findDOMNode to work on stateless components because that opens up it to abuse in normal application code. Are you open to allowing extra functions in TestUtils to simplify the process of testing stateless components? Or am I creating a code smell in my testing?

I’d be happy to create a PR for TestUtils if you think some more helper functions for stateless components would be beneficial.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
ngerritsencommented, Oct 3, 2015

I actually made a component that wraps a stateless component into a regular component (without any wrapper html to be added, the output should be the same)

https://github.com/ngerritsen/react-stateless-wrapper

With this you can just use the normal testing facilities.

1reaction
gaearoncommented, Jan 6, 2018

Seems like most people are pretty happy with Enzyme. I think we can close this.

If you have specific API proposals for React itself please create an RFC: https://github.com/reactjs/rfcs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing Easily Testable Code with Stateless Components in ...
We will look at stateless components and finish up with some unit tests in ... easy to test, which leads to better application...
Read more >
Testing state changes in React functional components
Learn how to test your React functional components and the state changes for components that use hooks with Jest and Enzyme as testing...
Read more >
React: Class Component vs Functional Component - ITNEXT
A functional(a.k.a. stateless) component is just a plain javascript function which takes props as an argument and returns a react element.
Read more >
Stateful vs. Stateless Functional Components in React - Code
There are a lot of benefits if you decide to use stateless functional components here; they are easy to write, understand, and test,...
Read more >
Learn Stateful and Stateless components in ReactJS - CronJ
A functional component is always a stateless component, but the class component can be stateless or stateful. There are many distinct names to...
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