Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add single versioned implementation of act for DevTools tests #28186

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.