Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab a container when wrapping render call into act?

render utility of React Testing Library among other things returns a container, that can be used to manually query the constructed DOM snippet. However when render call is wrapped into an act utility (to avoid dreadful When testing, code that causes React state updates should be wrapped into act(...) warnings), return value is undefined. I find this not intuitive. Is it expected? How would one gain access to the container (and other utilities) when using act?

like image 481
jayarjo Avatar asked Sep 20 '25 16:09

jayarjo


1 Answers

You can access all the destructured values by declaring them as a let variable outside your test and then using them in your render statement:

let debug;
let container;

it('renders', async () => {
    await act( async () => ({ container, debug } = render(<MyComponent />)));
    debug(container);
});
like image 179
Avi Avatar answered Sep 23 '25 13:09

Avi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!