react-testing-library: The current testing environment is not configured to support act(...) with vitest and React 18
“@testing-library/jest-dom”: “5.16.4”, “@testing-library/react”: “13.1.1”, DOM Environment: jsdom or happy-dom (happens with both)
Problem description:
After upgrading to @testing-library/react@13.x and react@18.x with vitest. A lot of react tests throw the following warning:
Warning: The current testing environment is not configured to support act(...)
I noticed this after switching from jest to vitest, and not changing anything else. (The tests do not show this warning when using jest, but they they show it when using vitest in the same project).
I previously had an unrelated issue causing the same warning with Jest: https://github.com/testing-library/react-testing-library/issues/1057
I resolved that, by removing a bad implementation of act(). However this time the tests are identical, and works with 1 test-runner and not with another.
Reproduction:
Credit to @stuarthallows for creating a repo that reproduces the problem: https://github.com/stuarthallows/vitest-error
@stuarthallows and @dar5hak also both seem to be having the same problem as me (see my other issue here: https://github.com/testing-library/react-testing-library/issues/1057 for more details).
Additional information.
In my case, this is breaking unit tests completely because it logs thousands of these warnings and it just stops after around 10 tests are completed.
For me a lot of the errors start with rc-motion
Warning: The current testing environment is not configured to support act(...)
at Align (/Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-align@4.0.12_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-align/lib/Align.js:45:23)
at DomWrapper (/Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-motion@2.5.1_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-motion/lib/DomWrapper.js:28:34)
at /Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-motion@2.5.1_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-motion/lib/CSSMotion.js:57:32
Which is probably running an animation with antd components. But I’m 99% sure that’s a red-herring since the same tests and components work just fine with jest. (or testing-library/react@12 and react@17)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 38
- Comments: 21 (2 by maintainers)
I raised a similar issue at vitest. But they think (and I kind of agree), that it has to be solved in testing-library.
tldr; of my vitest-issue:
testing-library/reactresolvesIS_REACT_ACT_ENVIRONMENTit uses the first existing ofself,windowandglobalas global-object, which in may case isselfreactaccessIS_REACT_ACT_ENVIRONMENTdirectly, which is probablyglobalThisorgloballink.vitest/jsdom,globalThisandselfare not the same object.The fix
I think the fix is to add
here: https://github.com/testing-library/react-testing-library/blob/c8c93f83228a68a270583c139972e79b1812b7d3/src/act-compat.js#L5-L28.
The workaround
The workaround is define some kind of proxy-property on global this, that will make sure that
selfandglobalThishave the same value ofIS_REACT_ACT_ENVIRONMENTthe code insetupFile.ts:I don’t know if it helps, but I had the same error without using Vitest
Warning: The current testing environment is not configured to support act(...)And
global.IS_REACT_ACT_ENVIRONMENT = true;didn’t work for meThis paragraph right here was a life-saver after two solid days of fighting through a react 18 upgrade.
Apparently, if you put
find*calls inside anact, the timing somehow doesn’t work out… it would be reasonable to think “more awaits better” in terms of timing, but for some reason, awaiting anactthat awaits things doesn’t work out well.The results are extremely confusing and spooky - literally your test can pass and then error after they pass!
This particular project is open source, and you can see the commit that fixed my failing tests here - it has several examples of “things that don’t work if you wrap them in act”:
https://github.com/abtion/muffi.net/commit/b70302f1871e2382025d2f6975ec12786dbcf299
Simply removing some of the
actwrappers fixes the timing issues.This should be covered by documentation maybe?
I know you’re just pulling this functionality from react’s own facilities, but their documentation doesn’t cover this - and I suppose maybe their documentation can’t be expected to cover the interplay with facilities provided by react-testing-library?
Not sure. This was extremely confusing and time consuming and seems like something almost everyone would run into at first.
Could it be detected and guarded against, with a better error message to guide people along maybe? 🤔
The workaround doesn’t totally work for me. The initial error
The current testing environment is not configured... to support act(...)goes away, but then I getactwarnings I wouldn’t have otherwise.I’ve made a reproduction here: https://github.com/airjp73/vitest-act-reproduction
Worth noting that this also appears to be exclusive to React 18. If you downgrade everything to React 17 (including downgrading this library to v12), the act warnings go away and you also don’t need to override the global anymore.
@binhphan009 You’re welcome, but in fact, a couple of months after I left that comment here, I re-visited my test suites and removed almost all of the
actcalls.It turns out I wasn’t properly waiting for my API-dependent components to load (I use react-query and I obviously mock the backend in these tests, but the wait still needed to be handled) so jest timed out because the UI wasn’t ready.
Wrapping
user.clickin awaitForand then again in anactis a bad workaround (as @nknapp rightfully points out). btw, the curly braces are ANOTHER bad workaround: they just cause the callback provided towaitForto NOT return the promise fromuser.clickwhich slightly alters its behavior in our favor, but it’s just an accident, not a proper solution.I’m now back to using
await user.click(screen.getByRole('button'))totally error-free.had this error which randomly failed tests. in my case I had code like this (select an option in dropdown):
solution:
I found a workaround that seems to work.
If you manually add
To the
setupFilethe warning will go away.This should of course be handled by
React testing libraryso it’s not a proper fix. But it allows us to use vitest until this issue is resolved 😃Thanks for creating that MR, @eps1lon. I think the issue I mentioned is indeed fixed with vitest@0.12.6
@stuarthallows I have just spent a couple of hours trying to find out why I also still get some
act-warnings. Maybe you have a similar problem.The following code is wrong, but the error is very hard to see
There is an
await act(), but the callback is a synchronous function. This leads to some bad “timing”, so that the actual state-change is just after theact()call, but before thewaitFor-call that is done insidewaitForLogout()The function
clickLogoutButtonshould be either completely synchronous:or completely asynchronous
Update: There are some eslint-rules, for TypeScript, that can detect such errors. I added the rules require-await and @typescript-eslint/await-thenable to my project.
Tbh. I don’t think it’s good practice to put actions like ‘click’ into a ‘waitfor’.
waitForis for observation. It may end up in multiple clicks if you are not careful.waitForshould also not be nested in ‘act’.Those may be pragmatic workarounds, but not proper solutions.
I am writing this for everybody who is new to testing-library, reading this thread and learning from it.
If these problems still occur, it’s probably a bug, in testing-library or vitest.
With React 17 + @TL/react 12 + @TL/user-event 13 I had tests such as this:
Worked just fine. I was proud of not using
acteven once in the entire project. (btw, note my test is synchronous. async/await was only needed with more complex components)I’m working on upgrade to React 18 + @TL/react 13 + @TL/user-event 14 👷 Following the recommendation in the docs I switched out of
userEvent:For most (simple) tests, this was fixed by adding
waitFor:However, the more complex components would throw ‘An update to Comp inside a test was not wrapped in act’. Seems weird because I was under the impression the
waitFordoes that under the hood. Swallowed my pride 😞 and fixed it by addingact:Surprisingly in some cases I get ‘The current testing environment is not configured to support act’ (sadly I can’t tell why it warns for certain components but not for others). After hours of trying to fix it I found a solution completely by accident:
I hope this is helpful - either to the wonderful maintainers of TL or any user facing the same warning. 🙏
EDIT: I’m aware the callback in
waitForcould be eithercallback: () => (void | Promise<void>). Still wrapping my head around all of this.UPDATE: Some of my issues were because I
useFakeTimers- this was solved byuserEvent.setup({advanceTimers: jest.advanceTimersByTime}). I still faced many “testing environment is not configured to support act” warnings (mainly in components that use react-query 🤔) and ended up overridingconsole.errorto ignore them.FINAL(?) EDIT (2023-02-06): All of the above is one long workaround for an error caused by me not properly waiting for my components to load. Don’t do it. Read my other comment.
Please don’t do this! Read another comment I left below👇
@targumon So what were your findings and how did you resolve it? 😄
thanks @targumon
await act(() => waitFor(() => { user.click(screen.getByRole(‘button’)) })) // ✅ curly braces!
saved my life ❤️
@targumon you teased an answer but I am missing code! 😅