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

act shows up as deprecated when using react 18.3 #1316

Closed ambarvm closed 2 months ago

ambarvm commented 2 months ago

Relevant code or config:

import { act } from '@testing-library/react';
act(() => {});

Act is marked as deprecated.

The signature (callback: () => VoidOrUndefinedOnly): void of act is deprecated.

What you did:

Updated react to 18.3 in an existing project

What happened:

act imported from testing library is marked deprecated.

Reproduction:

https://stackblitz.com/edit/rtl-template-749zgo?file=src%2FApp.test.tsx

Problem description:

The js code correctly exports the new React.act which is not deprecated. https://github.com/testing-library/react-testing-library/blob/f6a1677501b53471f6a989078726aeb0dea114be/src/act-compat.js#L5 But the types file always exports the type of deprecated act. https://github.com/testing-library/react-testing-library/blob/f6a1677501b53471f6a989078726aeb0dea114be/types/index.d.ts#L10

Suggested solution:

Add another condition to the type definition to export React.act when it is present.

export const act: typeof reactAct extends undefined ? (typeof reactDomTestUtilsAct extends undefined
  ? (callback: () => void) => void
  : typeof reactDomTestUtilsAct) : typeof reactAct

I think we can also remove the case for act not being present since react peerDependency is ^18.

export const act: typeof reactAct extends undefined ? typeof reactDomTestUtilsAct : typeof reactAct
MatanBobi commented 2 months ago

Thanks @ambarvm, I've created https://github.com/testing-library/react-testing-library/pull/1319 to resolve this :)