-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Support for React 18 #2524
Comments
That’d be great, but first we’d need to resolve #2429. |
is there enzyme support for react 17 ? |
@mahdizaabi nope, see #2429 |
This comment has been minimized.
This comment has been minimized.
React 18 is now stable: https://reactjs.org/blog/2022/03/29/react-v18.html |
If you've got a large number of enzyme unit tests (> 1200 in our case) but you still want to switch to react 18 right now and migrate to react-testing-library progressively you can do it with this method (inspired by this article) :
It's not perfect but i think it's better than waiting for the migration of all our tests or for an hypothetical official (or unofficial) enzyme-adapter-react-18. |
Using
everything should work with react v18 and jest28 |
This comment was marked as spam.
This comment was marked as spam.
So, guys do we already have the solution how to use Enzyme in react 18? |
We have successfully used Packages with these versions which proven to be working together on a large amount of various tests. "devDependencies": {
"@cfaester/enzyme-adapter-react-18": "^0.5.1",
"@testing-library/react": "^13.3.0",
"@types/enzyme": "^3.10.12",
"enzyme": "^3.11.0",
"enzyme-to-json": "^3.6.1",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"react": "^18.2.0",
"react-docgen-typescript": "^2.2.2",
"react-dom": "^18.2.0",
"ts-react-display-name": "^1.2.2",
// Jest versions working with this setup in case you have issues
"@types/jest": "^27.5.2",
"jest": "^27.5.1",
"ts-jest": "^27.1.5",
} Generally, three methods are causing issues with new React: The nasty hack is to absolutely wait for all component updates before continuing executing the rest of the test. test-utils/src/testComponentHelper.ts import {
act,
} from '@testing-library/react';
import type {
CommonWrapper,
} from 'enzyme';
// State update magic happens here
const waitForComponentToPaint = async (wrapper: CommonWrapper): Promise<void> => {
await act(async () => {
await new Promise<void>((resolve) => {
window.setTimeout(
() => {
/*
* Make sure it is the last task in the queue.
* https://dmitripavlutin.com/javascript-promises-settimeout/
*/
window.setTimeout(
resolve,
1,
);
},
1,
);
});
});
wrapper.update();
};
export default waitForComponentToPaint; And then you do terrible things like these in your old tests: wrapper.setProps({ value: 'foobar' });
await waitForComponentToPaint(wrapper);
and
wrapper.find('input').simulate('change', { target: { files: mockFiles } });
await waitForComponentToPaint(wrapper);
and
const wrapper = mount(...);
// Wait even longer for complex component updates
await waitForComponentToPaint(wrapper);
await waitForComponentToPaint(wrapper); This is not perfect, but it works for old React 16/17 components and buys you time to migrate to React Testing Library. |
@igorpupkinable I like the solution. Do you have the configuration that you use for the adapter, and if you use jest, which version |
Standard adapter configuration as follows:
|
Thanks, @igorpupkinable. I initially used |
@eduardoacskimlinks thanks for the feedback. I have updated my answer accordingly. |
I would like to ask for support for React 18, as the alpha version was recently released: https://reactjs.org/blog/2021/06/08/the-plan-for-react-18.html
While this is an alpha release, my understanding is that this release is targeted towards library maintainers. The React team announced they're open for feedback:
I'm asking early, because it might be a good moment to start looking into React 18 adapter, and to try influence React in case there are major issues with onboarding Enzyme onto it.
The text was updated successfully, but these errors were encountered: