Skip to content

Commit f550c18

Browse files
Expand renderIntoDocument migration guidance
1 parent 40ea071 commit f550c18

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/content/warnings/react-dom-test-utils.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The React Team recommends migrating your tests to [@testing-library/react](https
2727
### ReactDOMTestUtils.renderIntoDocument {/*reactdomtestutilsrenderintodocument*/}
2828

2929
`renderIntoDocument` can be replaced with `render` from `@testing-library/react`.
30+
Unlike `renderIntoDocument`, `render` gives you access to the rendered container and explicit cleanup APIs.
3031

3132
Before:
3233

@@ -44,6 +45,22 @@ import {render} from '@testing-library/react';
4445
render(<Component />);
4546
```
4647

48+
`renderIntoDocument` rendered into a detached DOM node and returned the component instance instead of the container.
49+
This made it difficult to inspect the rendered output or reliably clean it up after a test.
50+
`render` returns helpers for the rendered tree, including `unmount`, and React Testing Library also provides `cleanup` to remove any trees mounted with `render` between tests.
51+
52+
```js
53+
import {render} from '@testing-library/react';
54+
55+
test('renders a component', () => {
56+
const {unmount} = render(<Component />);
57+
58+
// ...assertions...
59+
60+
unmount();
61+
});
62+
```
63+
4764
### ReactDOMTestUtils.Simulate {/*reactdomtestutilssimulate*/}
4865

4966
`Simulate` can be replaced with `fireEvent` from `@testing-library/react`.

0 commit comments

Comments
 (0)