-
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
Receiving 'event was not wrapped in act' warning after upgrading to React 18 #1216
Comments
can i work on this. |
@kentcdodds @eps1lon |
@alexandrsashin The current version of RTL already supports React 18. Having said that, we do have some work that @eps1lon has been working on in our alpha version, @atshakil, can you please upgrade to our alpha version to see if this resolves your issue? :) Thanks. |
@MatanBobi I tested using the PR build from @eps1lon. "@testing-library/react": "https://pkg.csb.dev/testing-library/react-testing-library/commit/b156f877/@testing-library/react" Looking forward to a release soon! :) |
@atshakil then you can close such issue.) |
Let's keep this one open until alpha will be merged to main :) |
Yeah this is one of the scenarios we'd like to address with #1214 but it needs some additional time. If you're blocked by this, you can always +import {cleanup} from '@testing-library/react';
expect(() => {
render(<CompC />);
}).not.toThrow();
+cleanup() |
I think I've encountered something similar to this. I've been working on upgrading one of our frontend projects to React 18 and I was a bit stumped seeing thousands of warning messages like the following when running the unit tests after upgrading the React and React Testing Library packages:
Our company has a very similar frontend project that made the jump to React 18 several months back and doesn't have this issue, so I was puzzled what the difference was. I've finally been able to narrow it down to what appears to be a regression between v13.4.0 and v14.0.0 of this package - or more specifically, between v13.5.0-alpha.1 (which is okay) and v14.0.0-alpha.1. With v13.5.0-alpha.1 or earlier, the output when running the same test is simply:
This was the component I was testing: import { useState } from 'react'
export function Temp() {
const [_, setFoo] = useState(false)
return (
<div>
<button role="button" onClick={() => setFoo(true)}>
Example
</button>
</div>
)
} This was my test case: import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Temp } from './Temp'
describe('Temp', () => {
it('foo', async () => {
render(<Temp />)
await userEvent.click(screen.getByRole('button', { name: 'Example' }))
})
}) |
@TAGC Yes, versions of libraries are important. When I had problems with Your test looks good and you can improve it using |
While searching for this error, I came across #1051 in my search first. I created a reproduction repo here: https://github.com/pzaczkiewicz-athenahealth/react-datepicker-user-event-test-failure However, I wasn't able to use https://pkg.csb.dev/testing-library/react-testing-library/commit/b156f877/@testing-library/react successfully. I had issues installing it with |
I'm going to log a separate issue, as my issue happens in the middle of |
We have stuck on 13.4 because of this issue. |
Yup, making sure there was only one version of |
Thank you so much! This solved it for me as well. Should I be using a different user-event version for 14.0.0? |
@btmnk if you use the lastest version of RTL I recommend using the latest version of |
But the latest version of I'm used to such library versions all being kept in sync but I guess that's not the case here.. (e.g. for every version of @testing-library/react there is also the same version of @testing-library/user-event so the compatibility is clear) // EDIT: Hmm I checked the repositories just now and user-event only uses the dom library as a peerDependency. So I assume the problem was the time when I installed each of the two packages. When I first installed @testing-library/react it probably installed a different version of dom than @testing-library/user-event did (since I installed that quite a while later). So nvm then, it was just an issue on my side. When installing both at the same time they should both use the same version of dom. |
Nowadays I think the best solution for any project - upgrade all @testing-library packages + eslint plugins, read migration guides (if it is needed) and check that only one common |
Hey, "devDependencies": {
"@testing-library/react": "^14.1.2",
"@testing-library/jest-dom": "^6.2.0",
"@testing-library/user-event": "^14.5.2",
"cross-fetch": "^4.0.0",
"eslint": "^8.56.0",
"eslint-config-react-app": "^7.0.1",
"jsdom": "^23.2.0",
"msw": "^2.0.13",
"vite-plugin-eslint": "^1.8.1",
"vitest": "^1.1.3"
} I'm using react it("should create a MOTO paymentxxx", async () => {
const {
user,
getByPlaceholderText,
getByRole,
} = setup();
// search for a customer
const lastNameInput = getByPlaceholderText("Last Name");
const firstNameInput = getByPlaceholderText("First Name");
await user.type(lastNameInput, "Miles"); // this produces the warning
});
function setup() {
const user = userEvent.setup();
const utils = render(
<CreatePayment />
);
return {
...utils,
user,
};
} As soon as I call Warning: An update to CustomerSearch 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://reactjs.org/link/wrap-tests-with-act
at CustomerSearch (/home/node/repo/apps/payments/src/features/customer-search/customer-search.component.jsx:16:29)
at div
at CreatePayment (/home/node/repo/apps/payments/src/features/create-payment/create-payment.component.jsx:18:43)
at QueryClientProvider (file:///home/node/repo/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@tanstack/react-query/build/lib/QueryClientProvider.mjs:41:3)
at Wrapper (/home/node/repo/apps/payments/src/features/create-payment/tests/create-payments-basic.test.jsx:1190:20) Please could someone help me how to solve this? The amount of warnings is really annoying. Thanks! |
@wuarmin
|
@alexandrsashin Thank you!! /@testing-library/[email protected]:
resolution: {integrity: sha512-fB0R+fa3AUqbLHWyxXa2kGVtf1Fe1ZZFr0Zp6AIbIAzXb2mKbEXl+PCQNUOaq5lbTab5tfctfXRNsWXxa2f7Aw==}
engines: {node: '>=14'}
dependencies:
'@babel/code-frame': 7.23.5
'@babel/runtime': 7.23.6
'@types/aria-query': 5.0.4
aria-query: 5.1.3
chalk: 4.1.2
dom-accessibility-api: 0.5.16
lz-string: 1.5.0
pretty-format: 27.5.1
dev: true
/@testing-library/[email protected]:
resolution: {integrity: sha512-FlS4ZWlp97iiNWig0Muq8p+3rVDjRiYE+YKGbAqXOu9nwJFFOdL00kFpz42M+4huzYi86vAK1sOOfyOG45muIQ==}
engines: {node: '>=14'}
dependencies:
'@babel/code-frame': 7.23.5
'@babel/runtime': 7.23.8
'@types/aria-query': 5.0.4
aria-query: 5.1.3
chalk: 4.1.2
dom-accessibility-api: 0.5.16
lz-string: 1.5.0
pretty-format: 27.5.1
dev: true I really don't know what the reason was, but somehow I had 2 versions of @testing-library/dom (9.3.3 and 9.3.4) in my workspace. I solved the problem as follows:
I don't understand how the problem was solved. Do you know? I haven't actually changed anything technically, just added the packaged and removed it after that. The only file that was changed after this procedure was pnpm.lock.yaml. There was now only one entry with version 9.3.4. Thanks |
Was having the same error. It turns out it was cuased by |
Any progress on this issue? |
For people struggling with "was not wrapped in act(...)" with fake timers I managed to fix the (thousands of issues) warnings by changing:
We have fake timers enabled globally with (see https://jestjs.io/docs/configuration#faketimers-object):
I haven't seen this approach mentioned in any of the issues referencing the act warning errors. Perhaps https://testing-library.com/docs/using-fake-timers/ should be updated to mention this? Found this blog post helpful in explaining |
@testing-library/react
version: 14.0.0Relevant code or config:
What you did:
Run test using
yarn test
from a CRA configured project.What happened:
Receiving warning,
Reproduction:
Sandbox: https://codesandbox.io/p/sandbox/demo-act-warning-nested-1p4vjn?selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A1%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A1%7D%5D&file=%2Fsrc%2FCompC.test.tsx%3A7%2C25
Problem description:
act()
warning started to appear after upgrading to React 18.Wrapping with
waitFor
approach (as suggested here) has the following drawback.Considering
lazy
is used on a shared component, we'll have to update all the tests for all the components that are using the shared component (even though those components are not usinglazy
directly). For a large codebase, we'll have to update tests for hundreds of components. Even if we have only one shared component using lazy, we'll have to update all the dependent component tests.Seems like a lot of work!
Could we somehow keep the behavior identical to that on React 16 (
act
warning is not raised without needingwaitFor
)? It'll save a lot of developer effort.Suggested solution:
No warning message
The text was updated successfully, but these errors were encountered: