Skip to content

Commit 6e038f9

Browse files
committed
chore: sync act implementation with RTL
1 parent 8d7448e commit 6e038f9

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/act.ts

Lines changed: 11 additions & 12 deletions
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 };

0 commit comments

Comments
 (0)