Skip to content

Latest commit

 

History

History
99 lines (67 loc) · 2.56 KB

react-dom-test-utils.md

File metadata and controls

99 lines (67 loc) · 2.56 KB
title react-dom/test-utils Deprecation Warnings

ReactDOMTestUtils.act() warning {/reactdomtestutilsact-warning/}

act from react-dom/test-utils has been deprecated in favor of act from react.

Before:

import {act} from 'react-dom/test-utils';

After:

import {act} from 'react';

Rest of ReactDOMTestUtils APIS {/rest-of-reactdomtestutils-apis/}

All APIs except act have been removed.

The React Team recommends migrating your tests to @testing-library/react for a modern and well supported testing experience.

ReactDOMTestUtils.renderIntoDocument {/reactdomtestutilsrenderintodocument/}

renderIntoDocument can be replaced with render from @testing-library/react.

Before:

import {renderIntoDocument} from 'react-dom/test-utils';

renderIntoDocument(<Component />);

After:

import {render} from '@testing-library/react';

render(<Component />);

render returns an unmount function and the rendered container:

const {unmount, container} = render(<Component />);

unmount();
expect(container.innerHTML).toBe('');

React Testing Library automatically cleans up rendered trees after each test when your test framework provides an afterEach hook. You can call unmount yourself when you need to verify cleanup during a test. Unmounting a tree also removes content rendered into a portal by that tree. If your test creates a separate React root, unmount that root separately.

 ### ReactDOMTestUtils.Simulate {/*reactdomtestutilssimulate*/}

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

Before:

```js
import {Simulate} from 'react-dom/test-utils';

const element = document.querySelector('button');
Simulate.click(element);

After:

import {fireEvent} from '@testing-library/react';

const element = document.querySelector('button');
fireEvent.click(element);

Be aware that fireEvent dispatches an actual event on the element and doesn't just synthetically call the event handler.

List of all removed APIs {/list-of-all-removed-apis-list-of-all-removed-apis/}

  • mockComponent()
  • isElement()
  • isElementOfType()
  • isDOMComponent()
  • isCompositeComponent()
  • isCompositeComponentWithType()
  • findAllInRenderedTree()
  • scryRenderedDOMComponentsWithClass()
  • findRenderedDOMComponentWithClass()
  • scryRenderedDOMComponentsWithTag()
  • findRenderedDOMComponentWithTag()
  • scryRenderedComponentsWithType()
  • findRenderedComponentWithType()
  • renderIntoDocument
  • Simulate