Skip to content

Commit 4c03bb6

Browse files
authored
[DevTools] ignore tests without reactVersion pragma if REACT_VERSION specified (#24555)
In DevTools tests, if the REACT_VERSION specified, we know this is a regression test (testing older React Versions). Because a lot of tests test the DevTools front end and we don't want to run them in the regression test scenario, we decided to only run tests that have the // @reactVersion pragma defined. Because if there are no tests specified, jest will fail, we also opt to use jest.skip to skip all the tests that we don't want to run for a specific React version istead. This PR makes this change.
1 parent 0ecb77d commit 4c03bb6

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed

packages/react-devtools-shared/src/__tests__/transform-react-version-pragma-test.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,40 @@ const semver = require('semver');
1010

1111
let shouldPass;
1212
let isFocused;
13+
let shouldIgnore;
1314
describe('transform-react-version-pragma', () => {
15+
const originalTest = test;
16+
1417
// eslint-disable-next-line no-unused-vars
1518
const _test_react_version = (range, testName, cb) => {
16-
test(testName, (...args) => {
17-
shouldPass = semver.satisfies('18.0.0', range);
19+
originalTest(testName, (...args) => {
20+
shouldPass = !!semver.satisfies('18.0.0', range);
1821
return cb(...args);
1922
});
2023
};
2124

2225
// eslint-disable-next-line no-unused-vars
2326
const _test_react_version_focus = (range, testName, cb) => {
24-
test(testName, (...args) => {
25-
shouldPass = semver.satisfies('18.0.0', range);
27+
originalTest(testName, (...args) => {
28+
shouldPass = !!semver.satisfies('18.0.0', range);
2629
isFocused = true;
2730
return cb(...args);
2831
});
2932
};
3033

34+
// eslint-disable-next-line no-unused-vars
35+
const _test_ignore_for_react_version = (testName, cb) => {
36+
originalTest(testName, (...args) => {
37+
shouldIgnore = true;
38+
shouldPass = false;
39+
return cb(...args);
40+
});
41+
};
42+
3143
beforeEach(() => {
3244
shouldPass = null;
3345
isFocused = false;
46+
shouldIgnore = false;
3447
});
3548

3649
// @reactVersion >= 17.9
@@ -124,4 +137,14 @@ describe('transform-react-version-pragma', () => {
124137
expect(shouldPass).toBe(true);
125138
expect(isFocused).toBe(true);
126139
});
140+
141+
test('ignore test if no reactVersion', () => {
142+
expect(shouldPass).toBe(false);
143+
expect(shouldIgnore).toBe(true);
144+
});
145+
146+
test.only('ignore focused test if no reactVersion', () => {
147+
expect(shouldPass).toBe(false);
148+
expect(shouldIgnore).toBe(true);
149+
});
127150
});

scripts/babel/transform-react-version-pragma.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function transform(babel) {
1919
// See info about semver ranges here:
2020
// https://www.npmjs.com/package/semver
2121
function buildGateVersionCondition(comments) {
22+
if (!comments) {
23+
return null;
24+
}
25+
2226
let conditions = null;
2327
for (const line of comments) {
2428
const commentStr = line.value.trim();
@@ -62,15 +66,15 @@ function transform(babel) {
6266
callee.name === 'fit'
6367
) {
6468
const comments = statement.leadingComments;
65-
if (comments !== undefined) {
66-
const condition = buildGateVersionCondition(comments);
67-
if (condition !== null) {
68-
callee.name =
69-
callee.name === 'fit'
70-
? '_test_react_version_focus'
71-
: '_test_react_version';
72-
expression.arguments = [condition, ...expression.arguments];
73-
}
69+
const condition = buildGateVersionCondition(comments);
70+
if (condition !== null) {
71+
callee.name =
72+
callee.name === 'fit'
73+
? '_test_react_version_focus'
74+
: '_test_react_version';
75+
expression.arguments = [condition, ...expression.arguments];
76+
} else {
77+
callee.name = '_test_ignore_for_react_version';
7478
}
7579
}
7680
break;
@@ -84,14 +88,17 @@ function transform(babel) {
8488
callee.property.name === 'only'
8589
) {
8690
const comments = statement.leadingComments;
87-
if (comments !== undefined) {
88-
const condition = buildGateVersionCondition(comments);
89-
if (condition !== null) {
90-
statement.expression = t.callExpression(
91-
t.identifier('_test_react_version_focus'),
92-
[condition, ...expression.arguments]
93-
);
94-
}
91+
const condition = buildGateVersionCondition(comments);
92+
if (condition !== null) {
93+
statement.expression = t.callExpression(
94+
t.identifier('_test_react_version_focus'),
95+
[condition, ...expression.arguments]
96+
);
97+
} else {
98+
statement.expression = t.callExpression(
99+
t.identifier('_test_ignore_for_react_version'),
100+
expression.arguments
101+
);
95102
}
96103
}
97104
break;

scripts/jest/config.build-devtools.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ module.exports = Object.assign({}, baseConfig, {
8989
setupFiles: [
9090
...baseConfig.setupFiles,
9191
require.resolve('./setupTests.build.js'),
92-
require.resolve(
93-
'../../packages/react-devtools-shared/src/__tests__/setupEnv.js'
94-
),
92+
require.resolve('./devtools/setupEnv.js'),
9593
],
9694
setupFilesAfterEnv: [
9795
require.resolve(

scripts/jest/devtools/preprocessor.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ const pathToTransformReactVersionPragma = require.resolve(
44
'../../babel/transform-react-version-pragma'
55
);
66

7+
function getDevToolsPlugins(filePath) {
8+
const plugins = [];
9+
if (
10+
process.env.REACT_VERSION ||
11+
filePath.match(/\/transform-react-version-pragma-test/)
12+
) {
13+
plugins.push(pathToTransformReactVersionPragma);
14+
}
15+
return plugins;
16+
}
17+
718
module.exports = {
8-
devtoolsPlugins: [pathToTransformReactVersionPragma],
19+
getDevToolsPlugins,
920
};

packages/react-devtools-shared/src/__tests__/setupEnv.js renamed to scripts/jest/devtools/setupEnv.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const semver = require('semver');
4-
const ReactVersion = require('../../../shared/ReactVersion');
4+
const ReactVersion = require('../../../packages/shared/ReactVersion');
55

66
const {
77
DARK_MODE_DIMMED_WARNING_COLOR,
@@ -30,21 +30,29 @@ global.process.env.LIGHT_MODE_DIMMED_LOG_COLOR = LIGHT_MODE_DIMMED_LOG_COLOR;
3030

3131
global._test_react_version = (range, testName, callback) => {
3232
const trimmedRange = range.replaceAll(' ', '');
33-
const reactVersion = process.env.REACT_VERSION || ReactVersion;
33+
const reactVersion = process.env.REACT_VERSION || ReactVersion.default;
3434
const shouldPass = semver.satisfies(reactVersion, trimmedRange);
3535

3636
if (shouldPass) {
3737
test(testName, callback);
38+
} else {
39+
test.skip(testName, callback);
3840
}
3941
};
4042

4143
global._test_react_version_focus = (range, testName, callback) => {
4244
const trimmedRange = range.replaceAll(' ', '');
43-
const reactVersion = process.env.REACT_VERSION || ReactVersion;
45+
const reactVersion = process.env.REACT_VERSION || ReactVersion.default;
4446
const shouldPass = semver.satisfies(reactVersion, trimmedRange);
4547

4648
if (shouldPass) {
4749
// eslint-disable-next-line jest/no-focused-tests
4850
test.only(testName, callback);
51+
} else {
52+
test.skip(testName, callback);
4953
}
5054
};
55+
56+
global._test_ignore_for_react_version = (testName, callback) => {
57+
test.skip(testName, callback);
58+
};

scripts/jest/preprocessor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const coffee = require('coffee-script');
77

88
const tsPreprocessor = require('./typescript/preprocessor');
99
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
10-
const {devtoolsPlugins} = require('./devtools/preprocessor.js');
10+
const {getDevToolsPlugins} = require('./devtools/preprocessor.js');
1111

1212
const pathToBabel = path.join(
1313
require.resolve('@babel/core'),
@@ -84,7 +84,7 @@ module.exports = {
8484
babelOptions.plugins
8585
);
8686
if (isTestFile && isInDevToolsPackages) {
87-
plugins.push(...devtoolsPlugins);
87+
plugins.push(...getDevToolsPlugins(filePath));
8888
}
8989
return babel.transform(
9090
src,

0 commit comments

Comments
 (0)