[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.
Luna Ruan committed
May 13, 2022 at 21:54 UTC
4c03bb6ed01a448185d9a1554229208a9480560d
6 files changed
+77
-30
packages/react-devtools-shared/src/__tests__/transform-react-version-pragma-test.js
+27
-4
@@ -10,27 +10,40 @@ const semver = require('semver');
10
11
let shouldPass;
12
let isFocused;
13
+let shouldIgnore;
14
describe('transform-react-version-pragma', () => {
15
+ const originalTest = test;
16
+
17
// eslint-disable-next-line no-unused-vars
18
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);
21
return cb(...args);
22
});
23
};
24
25
// eslint-disable-next-line no-unused-vars
26
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);
29
isFocused = true;
30
return cb(...args);
31
});
32
};
33
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
+
43
beforeEach(() => {
44
shouldPass = null;
45
isFocused = false;
46
+ shouldIgnore = false;
47
});
48
49
// @reactVersion >= 17.9
@@ -124,4 +137,14 @@ describe('transform-react-version-pragma', () => {
137
expect(shouldPass).toBe(true);
138
expect(isFocused).toBe(true);
139
});
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
+ });
150
});
scripts/babel/transform-react-version-pragma.js
+24
-17
@@ -19,6 +19,10 @@ function transform(babel) {
19
// See info about semver ranges here:
20
// https://www.npmjs.com/package/semver
21
function buildGateVersionCondition(comments) {
22
+ if (!comments) {
23
+ return null;
24
+ }
25
+
26
let conditions = null;
27
for (const line of comments) {
28
const commentStr = line.value.trim();
@@ -62,15 +66,15 @@ function transform(babel) {
66
callee.name === 'fit'
67
) {
68
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';
78
}
79
}
80
break;
@@ -84,14 +88,17 @@ function transform(babel) {
88
callee.property.name === 'only'
89
) {
90
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
+ );
102
}
103
}
104
break;
scripts/jest/config.build-devtools.js
+1
-3
@@ -89,9 +89,7 @@ module.exports = Object.assign({}, baseConfig, {
89
setupFiles: [
90
...baseConfig.setupFiles,
91
require.resolve('./setupTests.build.js'),
92
- require.resolve(
93
- '../../packages/react-devtools-shared/src/__tests__/setupEnv.js'
94
- ),
92
+ require.resolve('./devtools/setupEnv.js'),
93
],
94
setupFilesAfterEnv: [
95
require.resolve(
scripts/jest/devtools/preprocessor.js
+12
-1
@@ -4,6 +4,17 @@ const pathToTransformReactVersionPragma = require.resolve(
4
'../../babel/transform-react-version-pragma'
5
);
6
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
+
18
module.exports = {
8
- devtoolsPlugins: [pathToTransformReactVersionPragma],
19
+ getDevToolsPlugins,
20
};
scripts/jest/devtools/setupEnv.js
renamed
+11
-3
@@ -1,7 +1,7 @@
1
'use strict';
2
3
const semver = require('semver');
4
-const ReactVersion = require('../../../shared/ReactVersion');
4
+const ReactVersion = require('../../../packages/shared/ReactVersion');
5
6
const {
7
DARK_MODE_DIMMED_WARNING_COLOR,
@@ -30,21 +30,29 @@ global.process.env.LIGHT_MODE_DIMMED_LOG_COLOR = LIGHT_MODE_DIMMED_LOG_COLOR;
30
31
global._test_react_version = (range, testName, callback) => {
32
const trimmedRange = range.replaceAll(' ', '');
33
- const reactVersion = process.env.REACT_VERSION || ReactVersion;
33
+ const reactVersion = process.env.REACT_VERSION || ReactVersion.default;
34
const shouldPass = semver.satisfies(reactVersion, trimmedRange);
35
36
if (shouldPass) {
37
test(testName, callback);
38
+ } else {
39
+ test.skip(testName, callback);
40
}
41
};
42
43
global._test_react_version_focus = (range, testName, callback) => {
44
const trimmedRange = range.replaceAll(' ', '');
43
- const reactVersion = process.env.REACT_VERSION || ReactVersion;
45
+ const reactVersion = process.env.REACT_VERSION || ReactVersion.default;
46
const shouldPass = semver.satisfies(reactVersion, trimmedRange);
47
48
if (shouldPass) {
49
// eslint-disable-next-line jest/no-focused-tests
50
test.only(testName, callback);
51
+ } else {
52
+ test.skip(testName, callback);
53
}
54
};
55
+
56
+global._test_ignore_for_react_version = (testName, callback) => {
57
+ test.skip(testName, callback);
58
+};
scripts/jest/preprocessor.js
+2
-2
@@ -7,7 +7,7 @@ const coffee = require('coffee-script');
7
8
const tsPreprocessor = require('./typescript/preprocessor');
9
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
10
-const {devtoolsPlugins} = require('./devtools/preprocessor.js');
10
+const {getDevToolsPlugins} = require('./devtools/preprocessor.js');
11
12
const pathToBabel = path.join(
13
require.resolve('@babel/core'),
@@ -84,7 +84,7 @@ module.exports = {
84
babelOptions.plugins
85
);
86
if (isTestFile && isInDevToolsPackages) {
87
- plugins.push(...devtoolsPlugins);
87
+ plugins.push(...getDevToolsPlugins(filePath));
88
}
89
return babel.transform(
90
src,