Skip to content

Commit 65d95b8

Browse files
authored
fix: gate react/jsx-runtime upgrade only for React >= 17 tests (#28256)
#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 ```
1 parent 103cddb commit 65d95b8

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
2828
"@babel/plugin-transform-object-super": "^7.10.4",
2929
"@babel/plugin-transform-parameters": "^7.10.5",
30+
"@babel/plugin-transform-react-jsx-source": "^7.10.5",
3031
"@babel/plugin-transform-react-jsx": "^7.23.4",
3132
"@babel/plugin-transform-react-jsx-development": "^7.22.5",
3233
"@babel/plugin-transform-shorthand-properties": "^7.10.4",

scripts/jest/devtools/setupEnv.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ global.process.env.LIGHT_MODE_DIMMED_ERROR_COLOR =
3131
LIGHT_MODE_DIMMED_ERROR_COLOR;
3232
global.process.env.LIGHT_MODE_DIMMED_LOG_COLOR = LIGHT_MODE_DIMMED_LOG_COLOR;
3333

34+
const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;
35+
3436
global._test_react_version = (range, testName, callback) => {
35-
const reactVersion = process.env.REACT_VERSION || ReactVersion;
36-
const shouldPass = semver.satisfies(reactVersion, range);
37+
const shouldPass = semver.satisfies(ReactVersionTestingAgainst, range);
3738

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

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

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

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

8183
function lazyRequireFunctionExports(moduleName) {
8284
jest.mock(moduleName, () => {

scripts/jest/preprocessor.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const hermesParser = require('hermes-parser');
88

99
const tsPreprocessor = require('./typescript/preprocessor');
1010
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
11+
const {ReactVersion} = require('../../ReactVersions');
12+
const semver = require('semver');
1113

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

34+
const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;
35+
3236
const babelOptions = {
3337
plugins: [
3438
// For Node environment only. For builds, Rollup takes care of ESM.
@@ -81,14 +85,23 @@ module.exports = {
8185
plugins.push(pathToTransformReactVersionPragma);
8286
}
8387

84-
plugins.push([
85-
process.env.NODE_ENV === 'development'
86-
? require.resolve('@babel/plugin-transform-react-jsx-development')
87-
: require.resolve('@babel/plugin-transform-react-jsx'),
88-
// The "automatic" runtime corresponds to react/jsx-runtime. "classic"
89-
// would be React.createElement.
90-
{runtime: 'automatic'},
91-
]);
88+
// This is only for React DevTools tests with React 16.x
89+
// `react/jsx-dev-runtime` and `react/jsx-runtime` are included in the package starting from v17
90+
if (semver.gte(ReactVersionTestingAgainst, '17.0.0')) {
91+
plugins.push([
92+
process.env.NODE_ENV === 'development'
93+
? require.resolve('@babel/plugin-transform-react-jsx-development')
94+
: require.resolve('@babel/plugin-transform-react-jsx'),
95+
// The "automatic" runtime corresponds to react/jsx-runtime. "classic"
96+
// would be React.createElement.
97+
{runtime: 'automatic'},
98+
]);
99+
} else {
100+
plugins.push(
101+
require.resolve('@babel/plugin-transform-react-jsx'),
102+
require.resolve('@babel/plugin-transform-react-jsx-source')
103+
);
104+
}
92105

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

0 commit comments

Comments
 (0)