Skip to content

Async data load with useEffect still triggers act warning #200

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

Closed
CodingItWrong opened this issue Jul 9, 2019 · 4 comments
Closed

Async data load with useEffect still triggers act warning #200

CodingItWrong opened this issue Jul 9, 2019 · 4 comments

Comments

@CodingItWrong
Copy link
Contributor

Versions

  • React: 16.8.6
  • React Native: 0.60.0
  • RNTL: 1.10.0
  • React-Test-Renderer: 16.8.6

Description

I'm attempting to test asynchronous data loads within useEffect, and I'm getting that old React "An update to [your component] inside a test was not wrapped in act(...)"

I see in #111 that changes were merged related to adding act to various RNTL calls, but that doesn't seem to address this case.

I finally dug through the master React issue on the act warnings, and my main takeaways are:

  • For now, use Jest's useFakeTimers and runAllTimers—this is the way Facebook does it and it should work.
  • In React 16.9.0 (not released in production yet either for the web or for React Native), act has been updated to support async/await, and so you will be able to do all your stuff in the test inside act, and assert outside of it. Just not released yet.

I tried the "the way Facebook does it" fix and it doesn't seem to work (see reproducible demo below). Do you know of a way to get the test to correctly run this code through act such that I don't get the warning?

Reproducible Demo

https://github.com/CodingItWrong/TestingRNHooks

@thymikee
Copy link
Member

thymikee commented Jul 9, 2019

Gonna have a look tomorrow, thanks for the report!

@thymikee
Copy link
Member

Unfortunately, there's nothing on our side we can do. This is a react-test-renderer issue and needs to be fixed upstream, as said by Sunil: facebook/react#14769 (comment). You can apply various workarounds in your testing environment though:

  • mock console.error and filter out those messages for now
  • transform async/await to global.Promise and mock it so that it wraps stuff with act, ideally as a helper imported only in React-based tests

However, personally, I try to ignore these warnings 🤷‍♂.

Also, please use await waitForElement(() => getByText('foo')) instead of await flushMicrotasksQueue() – it's easier to read intentions and you don't need to care if async actions take 1 or more microtask ticks.

@neiker
Copy link

neiker commented Aug 1, 2019

I added this to my jest setup file, maybe someone find it useful:

const originalConsoleError = console.error;

console.error = (...args) => {
  if (
    !args[0].startsWith(
      'Warning: An update to %s inside a test was not wrapped in act(...).',
    )
  ) {
    originalConsoleError(...args);
  }
};

@thymikee
Copy link
Member

thymikee commented Aug 1, 2019

Thanks @neiker, appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants