Skip to content

Commit

Permalink
chore: add single versioned implementation of act for DevTools tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxyq committed Feb 2, 2024
1 parent fada283 commit 319ea12
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
40 changes: 32 additions & 8 deletions packages/react-devtools-shared/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,47 @@
* @flow
*/

import semver from 'semver';

import typeof ReactTestRenderer from 'react-test-renderer';

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

import {ReactVersion} from '../../../../ReactVersions';

const requestedReactVersion = process.env.REACT_VERSION || ReactVersion;
export function getActDOMImplementation(): () => void | Promise<void> {
// This is for React < 18, where act was distributed in react-dom/test-utils.
if (semver.lt(requestedReactVersion, '18.0.0')) {
const ReactDOMTestUtils = require('react-dom/test-utils');
return ReactDOMTestUtils.act;
}

const React = require('react');
// This is for React 18, where act was distributed in react as unstable.
if (React.unstable_act) {
return React.unstable_act;
}

// This is for React > 18, where act is marked as stable.
if (React.act) {
return React.act;
}

throw new Error("Couldn't find any available act implementation");
}

export function act(
callback: Function,
recursivelyFlush: boolean = true,
): void {
// act from react-test-renderer has some side effects on React DevTools
// it injects the renderer for DevTools, see ReactTestRenderer.js
const {act: actTestRenderer} = require('react-test-renderer');
// Use `require('react-dom/test-utils').act` as a fallback for React 17, which can be used in integration tests for React DevTools.
const actDOM =
require('react').act ||
require('react').unstable_act ||
require('react-dom/test-utils').act;
const actDOM = getActDOMImplementation();

actDOM(() => {
actTestRenderer(() => {
Expand All @@ -47,10 +71,10 @@ export async function actAsync(
cb: () => *,
recursivelyFlush: boolean = true,
): Promise<void> {
// act from react-test-renderer has some side effects on React DevTools
// it injects the renderer for DevTools, see ReactTestRenderer.js
const {act: actTestRenderer} = require('react-test-renderer');
// Use `require('react-dom/test-utils').act` as a fallback for React 17, which can be used in integration tests for React DevTools.
const actDOM =
require('react').unstable_act || require('react-dom/test-utils').act;
const actDOM = getActDOMImplementation();

await actDOM(async () => {
await actTestRenderer(async () => {
Expand Down
2 changes: 0 additions & 2 deletions scripts/jest/devtools/config.build-devtools-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ if (REACT_VERSION) {
'^react-dom/client$'
] = `<rootDir>/build/${NODE_MODULES_DIR}/react-dom`;
}

setupFiles.push(require.resolve('./setupTests.build-devtools-regression'));
}

module.exports = {
Expand Down
30 changes: 0 additions & 30 deletions scripts/jest/devtools/setupTests.build-devtools-regression.js

This file was deleted.

0 comments on commit 319ea12

Please sign in to comment.