Skip to content

Commit e90360b

Browse files
refactor(v13): use react act if available (#1695)
* chore: tweak peer deps limits * chore: sync act implementation with RTL * chore: update yarn.lock
1 parent 383c241 commit e90360b

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
"redent": "^3.0.0"
5353
},
5454
"peerDependencies": {
55-
"jest": ">=28.0.0",
56-
"react": ">=18.3.0",
57-
"react-native": ">=0.75",
58-
"react-test-renderer": ">=18.3.0"
55+
"jest": ">=29.0.0",
56+
"react": ">=18.2.0",
57+
"react-native": ">=0.71",
58+
"react-test-renderer": ">=18.2.0"
5959
},
6060
"peerDependenciesMeta": {
6161
"jest": {
@@ -94,5 +94,8 @@
9494
"publishConfig": {
9595
"registry": "https://registry.npmjs.org"
9696
},
97-
"packageManager": "yarn@4.4.0"
97+
"packageManager": "yarn@4.4.0",
98+
"engines": {
99+
"node": ">=18"
100+
}
98101
}

src/act.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// This file and the act() implementation is sourced from react-testing-library
2-
// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js
2+
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/types/index.d.ts
3+
import * as React from 'react';
34
import { act as reactTestRendererAct } from 'react-test-renderer';
45

5-
type ReactAct = typeof reactTestRendererAct;
6+
const reactAct = typeof React.act === 'function' ? React.act : reactTestRendererAct;
7+
type ReactAct = 0 extends 1 & typeof React.act ? typeof reactTestRendererAct : typeof React.act;
68

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

25-
// this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
2627
try {
2728
// The return value of `act` is always a thenable.
2829
let callbackNeedsToBeAwaited = false;
2930
const actResult = actImplementation(() => {
3031
const result = callback();
31-
if (
32-
result !== null &&
33-
typeof result === 'object' &&
34-
// @ts-expect-error this should be a promise or thenable
35-
// eslint-disable-next-line promise/prefer-await-to-then
36-
typeof result.then === 'function'
37-
) {
32+
// @ts-expect-error TS is too strict here
33+
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
3834
callbackNeedsToBeAwaited = true;
3935
}
4036
return result;
@@ -44,15 +40,17 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
4440
const thenable = actResult;
4541
return {
4642
then: (resolve: (value: never) => never, reject: (value: never) => never) => {
47-
// eslint-disable-next-line
43+
// eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
4844
thenable.then(
4945
// eslint-disable-next-line promise/always-return
5046
(returnValue) => {
5147
setIsReactActEnvironment(previousActEnvironment);
48+
// @ts-expect-error
5249
resolve(returnValue);
5350
},
5451
(error) => {
5552
setIsReactActEnvironment(previousActEnvironment);
53+
// @ts-expect-error
5654
reject(error);
5755
},
5856
);
@@ -71,7 +69,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
7169
};
7270
}
7371

74-
const act = withGlobalActEnvironment(reactTestRendererAct) as ReactAct;
72+
// @ts-expect-error
73+
const act = withGlobalActEnvironment(reactAct) as ReactAct;
7574

7675
export default act;
7776
export { setIsReactActEnvironment as setReactActEnvironment, getIsReactActEnvironment };

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2603,10 +2603,10 @@ __metadata:
26032603
strip-ansi: "npm:^6.0.1"
26042604
typescript: "npm:^5.5.4"
26052605
peerDependencies:
2606-
jest: ">=28.0.0"
2607-
react: ">=18.3.0"
2608-
react-native: ">=0.75"
2609-
react-test-renderer: ">=18.3.0"
2606+
jest: ">=29.0.0"
2607+
react: ">=18.2.0"
2608+
react-native: ">=0.71"
2609+
react-test-renderer: ">=18.2.0"
26102610
peerDependenciesMeta:
26112611
jest:
26122612
optional: true

0 commit comments

Comments
 (0)