Skip to content

Commit ce11195

Browse files
committed
chore: add single versioned implementation of act for DevTools tests
1 parent d29f7d9 commit ce11195

File tree

1 file changed

+46
-0
lines changed
  • packages/react-devtools-shared/src/__tests__

1 file changed

+46
-0
lines changed

packages/react-devtools-shared/src/__tests__/utils.js

+46
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
* @flow
88
*/
99

10+
import semver from 'semver';
11+
1012
import typeof ReactTestRenderer from 'react-test-renderer';
1113

1214
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
1315
import type Store from 'react-devtools-shared/src/devtools/store';
1416
import type {ProfilingDataFrontend} from 'react-devtools-shared/src/devtools/views/Profiler/types';
1517
import type {ElementType} from 'react-devtools-shared/src/frontend/types';
1618

19+
import {ReactVersion} from '../../../../ReactVersions';
20+
1721
export function act(
1822
callback: Function,
1923
recursivelyFlush: boolean = true,
@@ -73,6 +77,48 @@ export async function actAsync(
7377
}
7478
}
7579

80+
const requestedReactVersion = process.env.REACT_VERSION || ReactVersion;
81+
export async function actImplementation(callback: Function): Promise<void> {
82+
// This is for React < 18, where act was distributed in react-dom/test-utils.
83+
if (semver.lt(requestedReactVersion, '18.0.0')) {
84+
return require('react-dom/test-utils').act(callback);
85+
}
86+
87+
const React = require('React');
88+
// This is for React 18, where act was distributed in react as unstable.
89+
if (React.unstable_act) {
90+
return React.unstable_act(callback);
91+
}
92+
93+
// This is for React > 18, where act is marked as stable.
94+
if (React.act) {
95+
return React.act(callback);
96+
}
97+
98+
throw new Error("Couldn't find any available act implementation");
99+
}
100+
101+
export async function actModern(callback: Function): Promise<void> {
102+
const {act: actTestRenderer} = require('react-test-renderer');
103+
104+
// act from react-test-renderer has some side effects on React DevTools
105+
// it injects the renderer for DevTools, see ReactTestRenderer.js
106+
await actImplementation(() => {
107+
actTestRenderer(() => {
108+
callback();
109+
});
110+
});
111+
112+
// Flush Bridge operations
113+
while (jest.getTimerCount() > 0) {
114+
await actImplementation(() => {
115+
actTestRenderer(() => {
116+
jest.runAllTimers();
117+
});
118+
});
119+
}
120+
}
121+
76122
export function beforeEachProfiling(): void {
77123
// Mock React's timing information so that test runs are predictable.
78124
jest.mock('scheduler', () => jest.requireActual('scheduler/unstable_mock'));

0 commit comments

Comments
 (0)