Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export new act when available #1319

Merged
merged 3 commits into from May 1, 2024
Merged

Conversation

MatanBobi
Copy link
Member

Resolves #1316

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

Copy link

codesandbox-ci bot commented Apr 29, 2024

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 5ee9d3d:

Sandbox Source
react-testing-library-examples Configuration

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you test this?

@MatanBobi
Copy link
Member Author

MatanBobi commented Apr 30, 2024

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
Copy link
Member

eps1lon commented Apr 30, 2024

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
Copy link
Member

eps1lon commented Apr 30, 2024

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
Copy link
Member Author

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
Copy link
Member Author

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.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works: https://github.com/eps1lon/rtl-deprecated-act-repro

Only errors on an error specific to older versions of @types/react.

@eps1lon eps1lon merged commit b6e59f7 into main May 1, 2024
10 checks passed
@eps1lon eps1lon deleted the fixes/export-new-act-when-available branch May 1, 2024 18:29
@MatanBobi
Copy link
Member Author

@eps1lon Related docs PR:
testing-library/testing-library-docs#1390

@mwegter95
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

act shows up as deprecated when using react 18.3
3 participants