testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
18.84k stars 1.09k forks source link

`ReactDOMTestUtils.act` is deprecated #1322

Closed angelod1as closed 2 months ago

angelod1as commented 2 months ago

Relevant code or config:

This is the error in my console:

    Warning: `ReactDOMTestUtils.act` is deprecated in favor of `React.act`. Import `act` from `react` instead of `react-dom/test-utils`. See https://react.dev/warnings/react-dom-test-utils for more info.

      39 |   ui: React.ReactElement,
      40 |   options?: Omit<RenderOptions, "queries">,
    > 41 | ) => render(ui, { wrapper: AllTheProviders, ...options })
         |            ^
      42 |
      43 | export * from "@testing-library/react"
      44 |

      at printWarning (node_modules/react-dom/cjs/react-dom-test-utils.development.js:71:30)
      at error (node_modules/react-dom/cjs/react-dom-test-utils.development.js:45:7)
      at actWithWarning (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1736:7)
      at node_modules/@testing-library/react/dist/act-compat.js:63:25
      at renderRoot (node_modules/@testing-library/react/dist/pure.js:159:26)
      at render (node_modules/@testing-library/react/dist/pure.js:246:10)
      at customRender (src/tests/index.tsx:41:12)
      at Object.<anonymous> (src/components/common/input/Input.test.tsx:7:60)

This is my custom render.

import { render, RenderOptions } from "@testing-library/react"
import userEvent from "@testing-library/user-event"

const customRender = (
  ui: React.ReactElement,
  options?: Omit<RenderOptions, "queries">,
) => render(ui, { wrapper: AllTheProviders, ...options })

export * from "@testing-library/react"

export { customRender as render, userEvent }

What you did:

I have a test file that exports a custom render function.

What happened:

It was working properly and now it throws the error above.

Reproduction:

I couldn't reproduce :(

Problem description:

Apparently Testing Library is importing from react-dom/test-utils, which is deprecated.

From the docs:

ReactDOMTestUtils.act() 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';

Suggested solution:

Update the files to import act from react.

eps1lon commented 2 months ago

Fixed in @testing-library/react@^15.0.6