Error thrown - Warning: You called act(async () => ...) without await.
See original GitHub issueAsk your Question
I have a simple test that seems to generate the right snapshot at the end of execution, but throws me a console.error
during the execution.
Here are the steps to get to the expected component state:
- Load the component
- Wait for the
useEffect
asynchronous logic to be executed so that theSegmentedControlIOS
is exposed (testID
=recordTypePicker
) - Set the
selectedSegmentIndex
to"1"
- Wait for the component to re-render and all the async logic to be executed
- Assert that the rendered component includes
newSObjectLayout
testID
The test
import * as React from 'react';
import { fireEvent, render, waitFor } from 'react-native-testing-library';
import { NewSObjectContainer } from '../NewSObject';
describe('NewSObjectContainer', () => {
const setup = () => {
const route = { params: { sobject: 'Account' } };
const navigation = { setOptions: jest.fn() };
const container = render(<NewSObjectContainer route={route} navigation={navigation} />);
return { container };
};
it('should render a NewSObjectContainer - with page layout exposed', async () => {
const { container } = setup();
await waitFor(() => expect(container.getByTestId('recordTypePicker')).toBeTruthy());
const input = container.getByTestId('recordTypePicker');
fireEvent(input, 'onChange', { nativeEvent: { selectedSegmentIndex: 1 } });
await waitFor(() => expect(container.getByTestId('newSObjectLayout')).toBeTruthy());
expect(container.toJSON()).toMatchSnapshot();
});
});
The console log
./node_modules/.bin/jest src/containers/__tests__/NewSObject.spec.tsx --u
console.error
Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple act calls and mixing their scopes. You should - await act(async () => ...);
at CustomConsole.console.error (node_modules/react-native/Libraries/YellowBox/YellowBox.js:63:9)
at printWarning (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:120:30)
at error (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:92:5)
at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14953:13
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)
PASS src/containers/__tests__/NewSObject.spec.tsx
NewSObjectContainer
✓ should render a NewSObjectContainer - with page layout exposed (499ms)
› 1 snapshot written.
Snapshot Summary
› 1 snapshot written from 1 test suite.
Test Suites: 1 passed, 1 total
Tests: q passed, q total
Snapshots: 1 written, 1 passed, q total
Time: 4.865s, estimated 8s
Ran all test suites matching /src\/containers\/__tests__\/NewSObject.spec.tsx/i.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:49
- Comments:112 (18 by maintainers)
Top Results From Across the Web
React Native testing - act without await - Stack Overflow
console.error Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple ...
Read more >Understanding Act function | React Native Testing Library
React decides to show this error whenever it detects that async act() call has not been awaited. The exact reasons why you might...
Read more >React Testing Library and the “not wrapped in act” Errors
Case 1: Asynchronous Updates. Test code somehow triggered a testing component to update in an asynchronous way. Here is an example: const MyComponent...
Read more >Testing asynchronous behaviour in React - Moxio
We 're using renderHook from the react-hooks-testing-library to test our hooks. We use jest as a test ... Warning: Do not await the...
Read more >Async Methods - Testing Library
Wait until the callback does not throw an error. In this case, that means // it'll wait until the mock function has been...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve noticed that this error/warning happens when I use more than 1
await
within ait
statementI have had this problem come up in a variety of places and 99% of the time the problem was that I was waiting for synchronous code. That is why for some people the error went away, after they removed an
await
, it is a strong sign that the test only required oneawait
or that theawait
is in the wrong place. Something that works quite nicely in these situations is going through the test and remove allawait
s and then look at the code that is being tested to see where theasync
code is actually happening and to useawait
only once theasync
code is actually triggered. If the assertion still fails because the render did not finish have a look at the bottom examples, maybe they can help because they wait until something shows up or is finished running.The following three examples are the ways I tend to use await the most, maybe it helps clean up tests:
fireEvent
on the element. If you wait for an element to render with the below method and await the assertion as well, the error will probably come up. This is a classic case of unnecessary waiting that I describe at the top. Therefore, don’t wait for synchronous code.@KieranO547 though the code is quite simple two follow up question:
When you get that error is the code you posted the only code that is running? To make sure that is the case I would remove all other renderings and tests in the file and only run that one file. The weird thing about the error showing up for you is that the spec you posted seems to contain nothing that could trigger the error.
What does the code look like, which you are testing. Are there timeouts, state changes, animations etc. just the spec code is only one side of the medal and makes it very hard to debug.