Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  82] [ 5]  / answers: 1 / hits: 16772  / 5 Years ago, tue, september 17, 2019, 12:00:00

Using react and react-dom 16.9.0



I am getting this warning when I'm testing my react hooks:



console.error node_modules/react-dom/cjs/react-dom-test-utils.development.js:80
Warning: Do not await the result of calling act(...) with sync logic, it is not a Promise.


My test code (using jest with @testing-library/react)



...
await act( () => {
rerender(
<HookTester
promise={asyncFunction}
initialValue={'extra loading...'}
/>
);
});

expect(asyncFunction).toHaveBeenCalledTimes(2);
...


But if I don't await, then my expectation would be done too early.


More From » reactjs

 Answers
10

Oh! I got it!



It turns out the docs mention synchronous functions like this:



act( () => {
// ... some 'sync logic'
});


which you can't await.



But you can await an async function of course:



await act( async () => {
// ... some 'async logic'
});


This fixed the problem for me.


[#51650] Sunday, September 8, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carlton

Total Points: 373
Total Questions: 123
Total Answers: 97

Location: Saint Helena
Member since Wed, Nov 9, 2022
2 Years ago
;