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.

react-test-renderer: is possible to test lifecycle functions?

See original GitHub issue

Do you want to request a feature or report a bug?

Question

What is the current behavior?

I am trying to use react-test-renderer and I notice that the lifecycles methods (ex: componentDidMount) need to be fired manually - rendered.getInstance().componentDidMount(), what solves my problem but reveals an other: I want to use shallow render, to test only the component under test, but then react-test-renderer/shallow API is minimal for rendering.

What is the expected behavior?

I was expecting that react-test-renderer would support testing my component reaction to the different lifecycles and be capable to use shallow rendering. Is there some way to use react-test-renderer in this scenario? Or is better I move to another library like enzyme?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Aug 26, 2018

Test renderer has no opinions about whether your code is synchronous or asynchronous. Just like ReactDOM doesn’t care about this.

But as I am using create function from react-test-renderer I need to get the re-render version from my component, what I think is not possible

When you call setState your component will re-render. Both with ReactDOM and with test renderer.

2reactions
VCuzmincommented, Mar 28, 2019

But how I test componentDidUpdate? Please help me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >
Testing React Components with react-test-renderer, and the ...
react -test-renderer is a library for rendering React components to pure JavaScript objects, but it can do a lot more than creating objects....
Read more >
Unit Testing Behavior of React Components with Test-Driven ...
When we want to validate that the component's children are rendered, or if we need the full component lifecycle and environment runtime methods,...
Read more >
When testing a React component, is there any way to directly ...
I saw that Call a React component method from outside suggested this should be possible - simply render the component in the test...
Read more >
A Complete Guide to Testing React Hooks - Toptal
Hooks were introduced in React 16.8 in late 2018. They are functions that hook into a functional component and allow us to use...
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