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.

act does not support async callback

See original GitHub issue

Versions

"react": "16.8.3"
"react-native": "0.59.5"
"react-test-renderer": "16.8.3"
"react-native-testing-library": "1.7.0"

Description

Unable to use async function as act callback without console errors. Either a console.error is emitted when not using act in the test (see Demo 1) or the test fails by using async callback in act (see Demo 2)

Now supported in react-testing-library: https://github.com/testing-library/react-testing-library/releases/tag/v6.1.0

Demo 1 (not wrapping in act)

import { render, act } from 'react-native-testing-library';
import Dialog from '...';

const Component = () => {
  const [result, setResult] = React.useState(null);
  return (
    <View>
      <Text>{result}</Text>
      <Dialog
        onSubmit={async value => {
          setResult(await doSomethingAsync(value));
        }}
      />
    </View>
  );
};

// ... then in test
  const elem = render(<Component />);
  const dialog = elem.getByType(Dialog);
  await dialog.props.onSubmit('value');

Emits console error: <span style="color:red">

Warning: An update to Component inside a test was not wrapped in act(...).

When testing, code that causes React state updates should be wrapped into act(...):

act(() => {
  /* fire events that update state */
});
/* assert on the output */

This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
</span>

Demo 2 (wrapping with act -> test failure)

import { render, act } from 'react-native-testing-library';
import Dialog from '...';

const Component = () => {
  const [result, setResult] = React.useState(null);
  return (
    <View>
      <Text>{result}</Text>
      <Dialog
        onSubmit={async value => {
          setResult(await doSomethingAsync(value));
        }}
      />
    </View>
  );
};

// ... then in test
  const elem = render(<Component />);
  const dialog = elem.getByType(Dialog);
  await act(() => dialog.props.onSubmit('value'));

test failure with error: <span style="color:red">

Warning: The callback passed to TestRenderer.act(...) function must not return anything.

It looks like you wrote TestRenderer.act(async () => ...) or returned a Promise from it's callback. Putting asynchronous logic inside TestRenderer.act(...) is not supported.


console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:102
Warning: Do not await the result of calling TestRenderer.act(...), it is not a Promise.
</span>

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
alexanderkrumcommented, Jun 23, 2019

For now I am using the jest --silent to disable react logging

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing React: "Do not await the result of calling act(...) with ...
Oh! I got it! It turns out the docs mention synchronous functions like this: act( () => { // ... some 'sync logic'...
Read more >
React's sync and async `act`. One challenge of ... - Medium
Through a colleague at work, I recently found React 16.9 includes an act function that can take an async callback.
Read more >
std/asyncdispatch - Nim Programming Language
This module implements asynchronous IO. This includes a dispatcher, a Future type implementation, and an async macro which allows asynchronous code to be ......
Read more >
Testing-library: avoid these mistakes in async tests
It's an async RTL utility that accepts a callback and returns a promise. This promise is resolved as soon as the callback doesn't...
Read more >
Asynchronous Programming - Eloquent JavaScript
But that doesn't help when we want a single program to be able to make ... Performing multiple asynchronous actions in a row...
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