Skip to content

Commit edbfc63

Browse files
authored
Fix Jest cache for transform-react-version-pragma (#25712)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn debug-test --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary Jest caching wasn't working correctly for `transform-react-version-pragma`. One condition for including `transform-react-version-pragma` is that `process.env.REACT_VERSION` is set, but it wasn't included in the cache key computation. Thus local test runs would only run without `transform-react-version-pragma`, if jest runs weren't using the `-reactVersion` flag and then added it. Inlined the `scripts/jest/devtools/preprocessor.js` file, because it makes it more obvious that `process.env.REACT_VERSION` is used in `scripts/jest/preprocessor.js` <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> ## How did you test this change? <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. --> Repro step: - Clear jest cache - node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental --reactVersion 18.0 - node ./scripts/jest/jest-cli.js --build --project devtools --release-channel=experimental Before: Jest cached the first run with `REACT_VERSION` set, so in the second run `transform-react-version-pragma` is still there and runs only the regression tests on old react versions. After: - The second run runs all tests and ignore `// @reactVersion` as expected.
1 parent 15557fa commit edbfc63

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

scripts/jest/devtools/preprocessor.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

scripts/jest/preprocessor.js

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

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

1211
const pathToBabel = path.join(
1312
require.resolve('@babel/core'),
@@ -26,6 +25,9 @@ const pathToTransformInfiniteLoops = require.resolve(
2625
const pathToTransformTestGatePragma = require.resolve(
2726
'../babel/transform-test-gate-pragma'
2827
);
28+
const pathToTransformReactVersionPragma = require.resolve(
29+
'../babel/transform-react-version-pragma'
30+
);
2931
const pathToBabelrc = path.join(__dirname, '..', '..', 'babel.config.js');
3032
const pathToErrorCodes = require.resolve('../error-codes/codes.json');
3133

@@ -83,8 +85,13 @@ module.exports = {
8385
const plugins = (isTestFile ? testOnlyPlugins : sourceOnlyPlugins).concat(
8486
babelOptions.plugins
8587
);
86-
if (isTestFile && isInDevToolsPackages) {
87-
plugins.push(...getDevToolsPlugins(filePath));
88+
if (
89+
isTestFile &&
90+
isInDevToolsPackages &&
91+
(process.env.REACT_VERSION ||
92+
filePath.match(/\/transform-react-version-pragma-test/))
93+
) {
94+
plugins.push(pathToTransformReactVersionPragma);
8895
}
8996
return babel.transform(
9097
src,
@@ -103,12 +110,19 @@ module.exports = {
103110
return src;
104111
},
105112

106-
getCacheKey: createCacheKeyFunction([
107-
__filename,
108-
pathToBabel,
109-
pathToBabelrc,
110-
pathToTransformInfiniteLoops,
111-
pathToTransformTestGatePragma,
112-
pathToErrorCodes,
113-
]),
113+
getCacheKey: createCacheKeyFunction(
114+
[
115+
__filename,
116+
pathToBabel,
117+
pathToBabelrc,
118+
pathToTransformInfiniteLoops,
119+
pathToTransformTestGatePragma,
120+
pathToTransformReactVersionPragma,
121+
pathToErrorCodes,
122+
],
123+
[
124+
(process.env.REACT_VERSION != null).toString(),
125+
(process.env.NODE_ENV === 'development').toString(),
126+
]
127+
),
114128
};

0 commit comments

Comments
 (0)