react/scripts/babel/transform-react-version-pragma.js
Ruslan Lesiutin c8deb5db66
fix[ci]: fixed jest configuration not to skip too many devtools tests (#26955)
## Summary
Running `yarn test --project devtools --build` currently skips all
non-gated (without `@reactVersion` directives) devtools tests. This is
not expected behaviour, these changes are fixing it.

There were multiple related PRs to it:
- https://github.com/facebook/react/pull/26742
- https://github.com/facebook/react/pull/25712
- https://github.com/facebook/react/pull/24555

With these changes, the resulting behaviour will be:
- If `REACT_VERSION` env variable is specified:
    - jest will not include all non-gated test cases in the test run
- jest will run only a specific test case, when specified
`REACT_VERSION` value satisfies the range defined by `@reactVersion`
directives for this test case

- If `REACT_VERSION` env variable is not specified, jest will run all
non-gated tests:
   - jest will include all non-gated test cases in the test run
- jest will run all non-gated test cases, the only skipped test cases
will be those, which specified the range that does not include the next
stable version of react, which will be imported from `ReactVersions.js`

## How did you test this change?
Running `profilingCache` test suite without specifying `reactVersion`
now skips gated (>= 17 & < 18) test
<img width="1447" alt="Screenshot 2023-06-15 at 11 18 22"
src="https://github.com/facebook/react/assets/28902667/cad58994-2cb3-44b3-9eb2-1699c01a1eb3">

Running `profilingCache` test suite with specifying `reactVersion` to
`17` now runs this test case and skips others correctly
<img width="1447" alt="Screenshot 2023-06-15 at 11 20 11"
src="https://github.com/facebook/react/assets/28902667/d308960a-c172-4422-ba6f-9c0dbcd6f7d5">

Running `yarn test --project devtools ...` without specifying
`reactVersion` now runs all non-gated test cases
<img width="398" alt="Screenshot 2023-06-15 at 12 25 12"
src="https://github.com/facebook/react/assets/28902667/2b329634-0efd-4c4c-b460-889696bbc9e1">

Running `yarn test --project devtools ...` with specifying
`reactVersion` (to `17` in this example) now includes only gated tests
<img width="414" alt="Screenshot 2023-06-15 at 12 26 31"
src="https://github.com/facebook/react/assets/28902667/a702c27e-4c35-4b12-834c-e5bb06728997">
2023-06-22 09:33:05 +01:00

118 lines
3.6 KiB
JavaScript

'use strict';
/* eslint-disable no-for-of-loops/no-for-of-loops */
const getComments = require('./getComments');
const GATE_VERSION_STR = '@reactVersion ';
const REACT_VERSION_ENV = process.env.REACT_VERSION;
function transform(babel) {
const {types: t} = babel;
// Runs tests conditionally based on the version of react (semver range) we are running
// Input:
// @reactVersion >= 17.0
// test('some test', () => {/*...*/})
//
// Output:
// @reactVersion >= 17.0
// _test_react_version('>= 17.0', 'some test', () => {/*...*/});
//
// See info about semver ranges here:
// https://www.npmjs.com/package/semver
function buildGateVersionCondition(comments) {
if (!comments) {
return null;
}
let conditions = null;
for (const line of comments) {
const commentStr = line.value.trim();
if (commentStr.startsWith(GATE_VERSION_STR)) {
const condition = t.stringLiteral(
commentStr.slice(GATE_VERSION_STR.length)
);
if (conditions === null) {
conditions = [condition];
} else {
conditions.push(condition);
}
}
}
if (conditions !== null) {
let condition = conditions[0];
for (let i = 1; i < conditions.length; i++) {
const right = conditions[i];
condition = t.logicalExpression('&&', condition, right);
}
return condition;
} else {
return null;
}
}
return {
name: 'transform-react-version-pragma',
visitor: {
ExpressionStatement(path) {
const statement = path.node;
const expression = statement.expression;
if (expression.type === 'CallExpression') {
const callee = expression.callee;
switch (callee.type) {
case 'Identifier': {
if (
callee.name === 'test' ||
callee.name === 'it' ||
callee.name === 'fit'
) {
const comments = getComments(path);
const condition = buildGateVersionCondition(comments);
if (condition !== null) {
callee.name =
callee.name === 'fit'
? '_test_react_version_focus'
: '_test_react_version';
expression.arguments = [condition, ...expression.arguments];
} else if (REACT_VERSION_ENV) {
callee.name = '_test_ignore_for_react_version';
}
}
break;
}
case 'MemberExpression': {
if (
callee.object.type === 'Identifier' &&
(callee.object.name === 'test' ||
callee.object.name === 'it') &&
callee.property.type === 'Identifier' &&
callee.property.name === 'only'
) {
const comments = getComments(path);
const condition = buildGateVersionCondition(comments);
if (condition !== null) {
statement.expression = t.callExpression(
t.identifier('_test_react_version_focus'),
[condition, ...expression.arguments]
);
} else if (REACT_VERSION_ENV) {
statement.expression = t.callExpression(
t.identifier('_test_ignore_for_react_version'),
expression.arguments
);
}
}
break;
}
}
}
return;
},
},
};
}
module.exports = transform;