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.

Having to use an empty `act` immediately after render to avoid warning

See original GitHub issue

I have a component, which does some async loading of data from various sources - some coming from within the component itself, and some coming from a wrapped context - all take place in useEffect hooks. I’m finding I’m having to use empty act calls to avoid the act warning - contrived example:

const screen = render(<MyComponent />, { wrapper: <MyContextProviders />});

expect(await screen.findByText('Hello')).toBeTruthy();

In this example I get the “not wrapped in an act” warning from where one of the context providers is fetching data on mount. The odd thing, is the component doesn’t render Hello until this data fetching is finished and the fetched data is available.

To suppress the error I am having to add an empty act:

const screen = render(<MyComponent />, { wrapper: <MyContextProviders />});

await act(async () => {});

expect(await screen.findByText('Hello')).toBeTruthy();

I have no idea why but this seems to work. Could anyone help me understand why this works, or if there is a better way of doing this - as it feels a bit hacky. Thanks.

EDIT: Just remembered, the data loading function called by the provider is async - and once the data resolves it sets state, which is what is seemingly triggering the act warning.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AugustinLFcommented, Oct 7, 2022

Is there a workaround for older versions of RNTL

That’s going to be complicated, because RN 0.69 comes with react 18 which uses a different model for act, and we had to change quite a bit of things for it. You could try using a patch package and reimplement the changes, but that sounds like quite a hassle.

Regarding you RN reanimated issue, could a global mock work?

1reaction
leepowellnbscommented, Aug 19, 2022

@mdjastrzebski sure I’ll try to pull one together - I was trying to do so yesterday when I came across this strange behaviour https://github.com/callstack/react-native-testing-library/issues/1067

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preventing "not wrapped in act(...)" Jest warning when state ...
In this case, the response from getData() sets arr to the same value (an empty array) as I have initially set it to...
Read more >
Prevent `act` warning on async hook · Issue #14 - GitHub
It's fine and generates no warning if the async call is triggered by a previous update, because we can call 2 act ,...
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
Common mistakes with React Testing Library · Not using Testing Library ESLint plugins · Using wrapper as the variable name for the return...
Read more >
A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >
The last guide to the useEffect Hook you'll ever need
Effects are always executed after render, but you have options to opt out ... ESLint warnings) or carelessly use ESLint's disable comments; ...
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