-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
The current testing environment is not configured to support act(...) with vitest and React 18 #1061
Comments
I found a workaround that seems to work. If you manually add
To the This should of course be handled by |
The workaround doesn't totally work for me. The initial error 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. |
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:
The fixI think the fix is to add
here: react-testing-library/src/act-compat.js Lines 5 to 28 in c8c93f8
The workaroundThe workaround is define some kind of proxy-property on global this, that will make sure that
|
We're waiting on confirmation for vitest-dev/vitest#1146 (comment). We're matching the |
shouldn't a polyfill only apply if the feature itself is not available? I would argue that if |
We can do that. The code shouldn't necessarily be considered a polyfill. It worked perfectly well when We can still fix it by using |
I haven't had a chance to test myself, but I believe this was fixed on vitest's end. |
Yes can confirm that this issue is fixed for me with newest vitest version. |
Also not fixed for me with vitest 0.12.6, Nor does the workaround prevent the warnings. |
Thanks for creating that MR, @eps1lon. I think the issue I mentioned is indeed fixed with [email protected] @stuarthallows I have just spent a couple of hours trying to find out why I also still get some The following code is wrong, but the error is very hard to see it("sends no access token after logout", async() => {
// ...
await clickLogoutButton();
await waitForLogout();
// ...
})
async function clickLogoutButton() {
await act(() => {
return screen.getByText("Logout").click();
});
} There is an The function it("sends no access token after logout", async() => {
// ...
clickLogoutButton(); // <---- no "await"
await waitForLogout();
// ...
})
function clickLogoutButton() { // <--- no "async"
act(() => { // <--- no "await"
screen.getByText("Logout").click();
});
} or completely asynchronous it("sends no access token after logout", async() => {
// ...
await clickLogoutButton();
await waitForLogout();
// ...
})
async function clickLogoutButton() {
await act(async () => { // <--- added "async" to callback
screen.getByText("Logout").click();
});
} 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. |
This paragraph right here was a life-saver after two solid days of fighting through a react 18 upgrade. Apparently, if you put 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": Simply removing some of the 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? 🤔 |
With React 17 + @TL/react 12 + @TL/user-event 13 I had tests such as this: it('handles clicks', () => {
const spy = jest.fn()
render(<Button onClick={spy}/>)
userEvent.click(screen.getByRole('button'))
expect(spy).toBeCalled()
}) Worked just fine. I was proud of not using I'm working on upgrade to React 18 + @TL/react 13 + @TL/user-event 14 👷 const user = userEvent.setup()
await user.click(screen.getByRole('button')) // ❗️jest throws timeout error For most (simple) tests, this was fixed by adding await waitFor(() => user.click(screen.getByRole('button'))) However, the more complex components would throw 'An update to Comp inside a test was not wrapped in act'. await act(() => waitFor(() => user.click(screen.getByRole('button')))) // not as pretty as before, but works 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). await act(() => waitFor(() => { user.click(screen.getByRole('button')) })) // ✅ curly braces! 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 UPDATE: Some of my issues were because I 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👇 |
My case is if I run my test individually everything is good. But I run all of my tests at the same time this error comes up. |
thanks @targumon await act(() => waitFor(() => { user.click(screen.getByRole('button')) })) // ✅ curly braces! saved my life <3 |
Tbh. I don't think it's good practice to put actions like 'click' into a 'waitfor'.
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. |
I don't know if it helps, but I had the same error without using Vitest And |
@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 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 I'm now back to using |
@targumon So what were your findings and how did you resolve it? 😄 |
had this error which randomly failed tests. in my case I had code like this (select an option in dropdown):
solution:
|
const originalError = console.error; afterAll(() => { |
For those of you wondering changing the following
To this:
Fixed my issue. A.K.A. remove the act entirely and just |
@Danielvandervelden works indeed, dear legend. Tho the reason why this happens is nicely explained here, for those who have enough time to read through it: https://stackoverflow.com/questions/75448819/async-thunks-cause-warnings-in-tests-wrapped-in-act e.g https://codilime.com/blog/why-should-you-be-grateful-for-act-warnings-in-react-tests/ |
Hey guys! Idk if someone's still having problem with it, but |
"@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/[email protected] and [email protected] with
vitest
. A lot of react tests throw the following warning: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: #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: #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
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)
The text was updated successfully, but these errors were encountered: