Skip to content

Commit 08747ea

Browse files
committed
chore: migrate act from RTL
1 parent 12b4f76 commit 08747ea

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/act.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1+
import * as React from 'react';
12
// This file and the act() implementation is sourced from react-testing-library
23
// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js
3-
import { act as reactTestRendererAct } from 'react-test-renderer';
4-
import { checkReactVersionAtLeast } from './react-versions';
54

6-
type ReactAct = typeof reactTestRendererAct;
5+
type ReactAct = typeof React.act;
6+
7+
const reactAct = React.act;
8+
9+
function getGlobalThis() {
10+
/* istanbul ignore else */
11+
if (typeof globalThis !== 'undefined') {
12+
return globalThis;
13+
}
14+
/* istanbul ignore next */
15+
if (typeof self !== 'undefined') {
16+
return self;
17+
}
18+
/* istanbul ignore next */
19+
if (typeof window !== 'undefined') {
20+
return window;
21+
}
22+
/* istanbul ignore next */
23+
if (typeof global !== 'undefined') {
24+
return global;
25+
}
26+
/* istanbul ignore next */
27+
throw new Error('unable to locate global object');
28+
}
729

830
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
931
declare global {
1032
var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;
1133
}
1234

1335
function setIsReactActEnvironment(isReactActEnvironment: boolean | undefined) {
14-
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
36+
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
1537
}
1638

1739
function getIsReactActEnvironment() {
18-
return globalThis.IS_REACT_ACT_ENVIRONMENT;
40+
return getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
1941
}
2042

2143
function withGlobalActEnvironment(actImplementation: ReactAct) {
2244
return (callback: Parameters<ReactAct>[0]) => {
2345
const previousActEnvironment = getIsReactActEnvironment();
2446
setIsReactActEnvironment(true);
25-
26-
// this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
2747
try {
2848
// The return value of `act` is always a thenable.
2949
let callbackNeedsToBeAwaited = false;
3050
const actResult = actImplementation(() => {
3151
const result = callback();
32-
if (
33-
result !== null &&
34-
typeof result === 'object' &&
35-
// @ts-expect-error this should be a promise or thenable
36-
// eslint-disable-next-line promise/prefer-await-to-then
37-
typeof result.then === 'function'
38-
) {
52+
// @ts-expect-error result is not typed
53+
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
3954
callbackNeedsToBeAwaited = true;
4055
}
4156
return result;
@@ -44,8 +59,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
4459
if (callbackNeedsToBeAwaited) {
4560
const thenable = actResult;
4661
return {
47-
then: (resolve: (value: never) => never, reject: (value: never) => never) => {
48-
// eslint-disable-next-line
62+
then: (resolve: (value: unknown) => void, reject: (error: unknown) => void) => {
63+
// eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
4964
thenable.then(
5065
// eslint-disable-next-line promise/always-return
5166
(returnValue) => {
@@ -72,9 +87,7 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
7287
};
7388
}
7489

75-
const act: ReactAct = checkReactVersionAtLeast(18, 0)
76-
? (withGlobalActEnvironment(reactTestRendererAct) as ReactAct)
77-
: reactTestRendererAct;
90+
const act: ReactAct = withGlobalActEnvironment(reactAct) as ReactAct;
7891

7992
export default act;
8093
export { setIsReactActEnvironment as setReactActEnvironment, getIsReactActEnvironment };

src/helpers/wrap-async.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { checkReactVersionAtLeast } from '../react-versions';
66

77
/**
88
* Run given async callback with temporarily disabled `act` environment and flushes microtasks queue.
9+
* See: https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/pure.js#L37
910
*
1011
* @param callback Async callback to run
1112
* @returns Result of the callback

0 commit comments

Comments
 (0)