-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get how many times the component was rendered #135
Comments
I just figured out how this can be implemented and created proper PR #136 |
Closing for now. See comment in the PR for details. |
I'm using useContext for the first time. Things have gotten pretty complex with it due to my noviceship and I needed a way to add a simple count to my I thought it was going to be an excruciating challenge doing this in a react environment, but it wound up being really easy. I just render a
I remarked on my test setup here 3 years ago when I last had time to explore react. It's probably pretty out of date, but the gist is captured below: (LoginPage.test.js)
(test-utils.js)
I couldn't figure out how to merge "hooks" into render, but am open to alternative approaches to how modern testing would be done with react to keep an eye on these kinds of excess-render bugs. |
Describe the feature you'd like:
I would like to get inside
result
a property with number of times the component was rendered. It could be calledrenderCount
.The only drawback I can think about is performance issues, but I don't think a counter is a problem.
Suggested implementation:
One option could be wrapping the
setValue
passed toTestHook
so it increase an external count each time thansetValue
is called.Another option could be to pass an utils object to
TestHook
as achildren
with propertiessetValue
andincreaseCount
soTestHook
itself is in charge of increasing the count before callingchildren.setValue
.I think it's important increasing the count even if it fails, as in the end the component was triggered to be rendered.
Not really sure about where to init the count. Maybe a ref inside
TestHook
?Describe alternatives you've considered:
I'm trying to test a hook that returns a function to force a component to be re-rendered. I've tried to pass the count from outside but 1) I can't pass inner components, only outer (the wrapper) or 2) pass component prop but this is not possible.
I could build my own testing component with react-testing-library that calls the function returned by the hook when a button is clicked or something like that but I only have this library available in the project for now and I don't want to install RTL only for this test.
I can't think other way to test the hook I described.
Teachability, Documentation, Adoption, Migration Strategy:
I think is useful to get how many times the component was rendered for other purposes too: checking performance issues or memoized behaviors.
Would be easy to include in the docs, just adding a new element to renderHook Result section explaining what do you get for
renderCount
property.The text was updated successfully, but these errors were encountered: