Skip to content

refactor(v13): use react act if available #1695

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

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
"redent": "^3.0.0"
},
"peerDependencies": {
"jest": ">=28.0.0",
"react": ">=18.3.0",
"react-native": ">=0.75",
"react-test-renderer": ">=18.3.0"
"jest": ">=29.0.0",
"react": ">=18.2.0",
"react-native": ">=0.71",
"react-test-renderer": ">=18.2.0"
},
"peerDependenciesMeta": {
"jest": {
Expand Down Expand Up @@ -94,5 +94,8 @@
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
}
}
23 changes: 11 additions & 12 deletions src/act.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// This file and the act() implementation is sourced from react-testing-library
// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/types/index.d.ts
import * as React from 'react';
import { act as reactTestRendererAct } from 'react-test-renderer';

type ReactAct = typeof reactTestRendererAct;
const reactAct = typeof React.act === 'function' ? React.act : reactTestRendererAct;
type ReactAct = 0 extends 1 & typeof React.act ? typeof reactTestRendererAct : typeof React.act;

// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
declare global {
Expand All @@ -22,19 +24,13 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
const previousActEnvironment = getIsReactActEnvironment();
setIsReactActEnvironment(true);

// this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
try {
// The return value of `act` is always a thenable.
let callbackNeedsToBeAwaited = false;
const actResult = actImplementation(() => {
const result = callback();
if (
result !== null &&
typeof result === 'object' &&
// @ts-expect-error this should be a promise or thenable
// eslint-disable-next-line promise/prefer-await-to-then
typeof result.then === 'function'
) {
// @ts-expect-error TS is too strict here
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
callbackNeedsToBeAwaited = true;
}
return result;
Expand All @@ -44,15 +40,17 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
const thenable = actResult;
return {
then: (resolve: (value: never) => never, reject: (value: never) => never) => {
// eslint-disable-next-line
// eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
thenable.then(
// eslint-disable-next-line promise/always-return
(returnValue) => {
setIsReactActEnvironment(previousActEnvironment);
// @ts-expect-error
resolve(returnValue);
},
(error) => {
setIsReactActEnvironment(previousActEnvironment);
// @ts-expect-error
reject(error);
},
);
Expand All @@ -71,7 +69,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
};
}

const act = withGlobalActEnvironment(reactTestRendererAct) as ReactAct;
// @ts-expect-error
const act = withGlobalActEnvironment(reactAct) as ReactAct;

export default act;
export { setIsReactActEnvironment as setReactActEnvironment, getIsReactActEnvironment };
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2603,10 +2603,10 @@ __metadata:
strip-ansi: "npm:^6.0.1"
typescript: "npm:^5.5.4"
peerDependencies:
jest: ">=28.0.0"
react: ">=18.3.0"
react-native: ">=0.75"
react-test-renderer: ">=18.3.0"
jest: ">=29.0.0"
react: ">=18.2.0"
react-native: ">=0.71"
react-test-renderer: ">=18.2.0"
peerDependenciesMeta:
jest:
optional: true
Expand Down
Loading