Skip to content

Commit

Permalink
fix: gate react/jsx-runtime upgrade only for React >= 17 tests (faceb…
Browse files Browse the repository at this point in the history
…ook#28256)

facebook#28252 broke RDT tests with React
16.x.

These changes gate the `jsx-runtime` upgrade only for cases when we are
testing against React >= 17.

Validated with:
```
 ./scripts/circleci/download_devtools_regression_build.js 16.0 --replaceBuild && node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 16.0 --ci && ./scripts/circleci/download_devtools_regression_build.js 16.5 --replaceBuild && node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 16.5 --ci && ./scripts/circleci/download_devtools_regression_build.js 16.8 --replaceBuild && node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 16.8 --ci && ./scripts/circleci/download_devtools_regression_build.js 17.0 --replaceBuild && node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 17.0 --ci && ./scripts/circleci/download_devtools_regression_build.js 18.0 --replaceBuild && node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 18.0 --ci
 ```
  • Loading branch information
hoxyq authored and AndyPengc12 committed Apr 15, 2024
1 parent c806829 commit 974f35b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@babel/plugin-transform-object-super": "^7.10.4",
"@babel/plugin-transform-parameters": "^7.10.5",
"@babel/plugin-transform-react-jsx-source": "^7.10.5",
"@babel/plugin-transform-react-jsx": "^7.23.4",
"@babel/plugin-transform-react-jsx-development": "^7.22.5",
"@babel/plugin-transform-shorthand-properties": "^7.10.4",
Expand Down
20 changes: 11 additions & 9 deletions scripts/jest/devtools/setupEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ global.process.env.LIGHT_MODE_DIMMED_ERROR_COLOR =
LIGHT_MODE_DIMMED_ERROR_COLOR;
global.process.env.LIGHT_MODE_DIMMED_LOG_COLOR = LIGHT_MODE_DIMMED_LOG_COLOR;

const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;

global._test_react_version = (range, testName, callback) => {
const reactVersion = process.env.REACT_VERSION || ReactVersion;
const shouldPass = semver.satisfies(reactVersion, range);
const shouldPass = semver.satisfies(ReactVersionTestingAgainst, range);

if (shouldPass) {
test(testName, callback);
Expand All @@ -43,8 +44,7 @@ global._test_react_version = (range, testName, callback) => {
};

global._test_react_version_focus = (range, testName, callback) => {
const reactVersion = process.env.REACT_VERSION || ReactVersion;
const shouldPass = semver.satisfies(reactVersion, range);
const shouldPass = semver.satisfies(ReactVersionTestingAgainst, range);

if (shouldPass) {
// eslint-disable-next-line jest/no-focused-tests
Expand All @@ -71,12 +71,14 @@ global._test_ignore_for_react_version = (testName, callback) => {
// Longer term we should migrate all our tests away from using require() and
// resetModules, and use import syntax instead so this kind of thing doesn't
// happen.
lazyRequireFunctionExports('react/jsx-dev-runtime');
if (semver.gte(ReactVersionTestingAgainst, '17.0.0')) {
lazyRequireFunctionExports('react/jsx-dev-runtime');

// TODO: We shouldn't need to do this in the production runtime, but until
// we remove string refs they also depend on the shared state object. Remove
// once we remove string refs.
lazyRequireFunctionExports('react/jsx-runtime');
// TODO: We shouldn't need to do this in the production runtime, but until
// we remove string refs they also depend on the shared state object. Remove
// once we remove string refs.
lazyRequireFunctionExports('react/jsx-runtime');
}

function lazyRequireFunctionExports(moduleName) {
jest.mock(moduleName, () => {
Expand Down
29 changes: 21 additions & 8 deletions scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const hermesParser = require('hermes-parser');

const tsPreprocessor = require('./typescript/preprocessor');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
const {ReactVersion} = require('../../ReactVersions');
const semver = require('semver');

const pathToBabel = path.join(
require.resolve('@babel/core'),
Expand All @@ -29,6 +31,8 @@ const pathToTransformReactVersionPragma = require.resolve(
const pathToBabelrc = path.join(__dirname, '..', '..', 'babel.config.js');
const pathToErrorCodes = require.resolve('../error-codes/codes.json');

const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;

const babelOptions = {
plugins: [
// For Node environment only. For builds, Rollup takes care of ESM.
Expand Down Expand Up @@ -81,14 +85,23 @@ module.exports = {
plugins.push(pathToTransformReactVersionPragma);
}

plugins.push([
process.env.NODE_ENV === 'development'
? require.resolve('@babel/plugin-transform-react-jsx-development')
: require.resolve('@babel/plugin-transform-react-jsx'),
// The "automatic" runtime corresponds to react/jsx-runtime. "classic"
// would be React.createElement.
{runtime: 'automatic'},
]);
// This is only for React DevTools tests with React 16.x
// `react/jsx-dev-runtime` and `react/jsx-runtime` are included in the package starting from v17
if (semver.gte(ReactVersionTestingAgainst, '17.0.0')) {
plugins.push([
process.env.NODE_ENV === 'development'
? require.resolve('@babel/plugin-transform-react-jsx-development')
: require.resolve('@babel/plugin-transform-react-jsx'),
// The "automatic" runtime corresponds to react/jsx-runtime. "classic"
// would be React.createElement.
{runtime: 'automatic'},
]);
} else {
plugins.push(
require.resolve('@babel/plugin-transform-react-jsx'),
require.resolve('@babel/plugin-transform-react-jsx-source')
);
}

let sourceAst = hermesParser.parse(src, {babel: true});
return {
Expand Down

0 comments on commit 974f35b

Please sign in to comment.