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

fix: export new act when available #1319

Closed MatanBobi closed 2 months ago

MatanBobi commented 2 months ago

Resolves https://github.com/testing-library/react-testing-library/issues/1316

What: This PR exports the correct act if available to avoid deprecation warnings.

codesandbox-ci[bot] commented 2 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5ee9d3d2be9f5c7126540c262e6d9ed1f60e1e2a:

Sandbox Source
react-testing-library-examples Configuration
MatanBobi commented 2 months ago

How did you test this?

I've used npm link locally to see that the error is there before I link and removed after I link. Having said that, I wasn't able to install an older type of react and react-dom (to see that the type is exported properly when act isn't exported from react), for some reason I always get the same d.ts files which show act from test-utils as deprecated and act from react exported so that's odd.

From your comment, I realized that we probably also need to define @types/react as a peer dependency now? I saw that it's a dependency for @types/react-dom so I'm not sure if we want to rely on that or not.

Edit: I think I understand what happened, since @types/react-dom is our dependency and not a peer yet, this scenario is impossible because the types are bundled in our code. I'm always getting the types we install as part of RTL (as long as it's not a peer).

eps1lon commented 2 months ago

Yeah I don't think this ever worked for versions where react-dom/test-utils didn't have act.

TypeScript should error if you try to import something that doesn't exist.

We probably need to @ts-ignore the React.act import and then check if it's any (typeof act extends never ? true : false) instead of undefined.

Let's double check this works in versions without React.act by forcing an older version of @types/react via overrides (npm) or resolutions (yarn, pnpm). Make sure to clear lockfile and node_modules if you use npm.

eps1lon commented 2 months ago

And we also need @types/react as an optional peer dependency. We always needed that though since we already use React.ReactNode. It just happened to work because TypeScript always makes namespaces available globally.

MatanBobi commented 2 months ago

Using overrides didn't help as I'm using link so the dependency is still taken locally. I've tried installing an older version of @types/react and @types/react-dom and link. Things seem to work. I've pushed the fixes. I'll also create a PR to the docs site to state that users will probably want to install @types/react as it's a peer dependency now.

MatanBobi commented 2 months ago

Following your idea @eps1lon, I've created this repo for repro: https://github.com/MatanBobi/rtl-deprecated-act-repro Main branch is old React (18.2.0) that shows no regression with type change, other two branches are:

  1. New React (18.3.1) without type change - shows deprecation message.
  2. New React (18.3.1) with type change - no deprecation message.
MatanBobi commented 2 months ago

@eps1lon Related docs PR: https://github.com/testing-library/testing-library-docs/pull/1390

mwegter95 commented 2 months ago

Yesss thank you. This helped me. When I saw your post, I downloaded the latest version and my program started running, no warning after that. Tyty

npm install @testing-library/react@latest @testing-library/jest-dom@latest