@samitouri / QOS-React / commits / 115cd12d9b

Add test run that uses www feature flags (#18234)

In CI, we run our test suite against multiple build configurations. For example, we run our tests in both dev and prod, and in both the experimental and stable release channels. This is to prevent accidental deviations in behavior between the different builds. If there's an intentional deviation in behavior, the test author must account for them. However, we currently don't run tests against the www builds. That's a problem, because it's common for features to land in www before they land anywhere else, including the experimental release channel. Typically we do this so we can gradually roll out the feature behind a flag before deciding to enable it. The way we test those features today is by mutating the `shared/ReactFeatureFlags` module. There are a few downsides to this approach, though. The flag is only overridden for the specific tests or test suites where you apply the override. But usually what you want is to run *all* tests with the flag enabled, to protect against unexpected regressions. Also, mutating the feature flags module only works when running the tests against source, not against the final build artifacts, because the ReactFeatureFlags module is inlined by the build script. Instead, we should run the test suite against the www configuration, just like we do for prod, experimental, and so on. I've added a new command, `yarn test-www`. It automatically runs in CI. Some of the www feature flags are dynamic; that is, they depend on a runtime condition (i.e. a GK). These flags are imported from an external module that lives in www. Those flags will be enabled for some clients and disabled for others, so we should run the tests against *both* modes. So I've added a new global `__VARIANT__`, and a new test command `yarn test-www-variant`. `__VARIANT__` is set to false by default; when running `test-www-variant`, it's set to true. If we were going for *really* comprehensive coverage, we would run the tests against every possible configuration of feature flags: 2 ^ numberOfFlags total combinations. That's not practical, though, so instead we only run against two combinations: once with `__VARIANT__` set to `true`, and once with it set to `false`. We generally assume that flags can be toggled independently, so in practice this should be enough. You can also refer to `__VARIANT__` in tests to detect which mode you're running in. Or, you can import `shared/ReactFeatureFlags` and read the specific flag you can about. However, we should stop mutating that module going forward. Treat it as read-only. In this commit, I have only setup the www tests to run against source. I'll leave running against build for a follow up. Many of our tests currently assume they run only in the default configuration, and break when certain flags are toggled. Rather than fix these all up front, I've hard-coded the relevant flags to the default values. We can incrementally migrate those tests later.

Andrew Clark committed Mar 6, 2020 at 09:29 UTC 115cd12d9bbb2bff303e52feb1394e3a2cef20ca
12 files changed +233 -9
.circleci/config.yml
+124
@@ -115,6 +115,106 @@ jobs:
115 RELEASE_CHANNEL: experimental
116 command: yarn test --maxWorkers=2
117
118 + test_source_www:
119 + docker: *docker
120 + environment: *environment
121 + steps:
122 + - checkout
123 + - *restore_yarn_cache
124 + - *run_yarn
125 + - run:
126 + environment:
127 + RELEASE_CHANNEL: stable
128 + command: yarn test-www --maxWorkers=2
129 +
130 + test_source_www_variant:
131 + docker: *docker
132 + environment: *environment
133 + steps:
134 + - checkout
135 + - *restore_yarn_cache
136 + - *run_yarn
137 + - run:
138 + environment:
139 + RELEASE_CHANNEL: stable
140 + command: yarn test-www-variant --maxWorkers=2
141 +
142 + test_source_www_prod:
143 + docker: *docker
144 + environment: *environment
145 + steps:
146 + - checkout
147 + - *restore_yarn_cache
148 + - *run_yarn
149 + - run:
150 + environment:
151 + NODE_ENV: production
152 + RELEASE_CHANNEL: stable
153 + command: yarn test-www --maxWorkers=2
154 +
155 + test_source_www_variant_prod:
156 + docker: *docker
157 + environment: *environment
158 + steps:
159 + - checkout
160 + - *restore_yarn_cache
161 + - *run_yarn
162 + - run:
163 + environment:
164 + NODE_ENV: production
165 + RELEASE_CHANNEL: stable
166 + command: yarn test-www-variant --maxWorkers=2
167 +
168 + test_source_www_experimental:
169 + docker: *docker
170 + environment: *environment
171 + steps:
172 + - checkout
173 + - *restore_yarn_cache
174 + - *run_yarn
175 + - run:
176 + environment:
177 + RELEASE_CHANNEL: experimental
178 + command: yarn test-www --maxWorkers=2
179 +
180 + test_source_www_variant_experimental:
181 + docker: *docker
182 + environment: *environment
183 + steps:
184 + - checkout
185 + - *restore_yarn_cache
186 + - *run_yarn
187 + - run:
188 + environment:
189 + RELEASE_CHANNEL: experimental
190 + command: yarn test-www-variant --maxWorkers=2
191 +
192 + test_source_www_prod_experimental:
193 + docker: *docker
194 + environment: *environment
195 + steps:
196 + - checkout
197 + - *restore_yarn_cache
198 + - *run_yarn
199 + - run:
200 + environment:
201 + NODE_ENV: production
202 + RELEASE_CHANNEL: experimental
203 + command: yarn test-www --maxWorkers=2
204 +
205 + test_source_www_variant_prod_experimental:
206 + docker: *docker
207 + environment: *environment
208 + steps:
209 + - checkout
210 + - *restore_yarn_cache
211 + - *run_yarn
212 + - run:
213 + environment:
214 + NODE_ENV: production
215 + RELEASE_CHANNEL: experimental
216 + command: yarn test-www-variant --maxWorkers=2
217 +
218 test_source_persistent:
219 docker: *docker
220 environment: *environment
@@ -384,6 +484,18 @@ workflows:
484 - test_source_persistent:
485 requires:
486 - setup
487 + - test_source_www:
488 + requires:
489 + - setup
490 + - test_source_www_variant:
491 + requires:
492 + - setup
493 + - test_source_www_prod:
494 + requires:
495 + - setup
496 + - test_source_www_variant_prod:
497 + requires:
498 + - setup
499 - build:
500 requires:
501 - setup
@@ -415,6 +527,18 @@ workflows:
527 - test_source_prod_experimental:
528 requires:
529 - setup
530 + - test_source_www_experimental:
531 + requires:
532 + - setup
533 + - test_source_www_variant_experimental:
534 + requires:
535 + - setup
536 + - test_source_www_prod_experimental:
537 + requires:
538 + - setup
539 + - test_source_www_variant_prod_experimental:
540 + requires:
541 + - setup
542 - build_experimental:
543 requires:
544 - setup
.eslintrc.js
+3 -5
@@ -11,10 +11,7 @@ const OFF = 0;
11 const ERROR = 2;
12
13 module.exports = {
14 - extends: [
15 - 'fbjs',
16 - 'prettier'
17 - ],
14 + extends: ['fbjs', 'prettier'],
15
16 // Stop ESLint from looking for a configuration file in parent folders
17 root: true,
@@ -147,7 +144,7 @@ module.exports = {
144 'scripts/**/*.js',
145 'packages/*/npm/**/*.js',
146 'packages/dom-event-testing-library/**/*.js',
150 - 'packages/react-devtools*/**/*.js'
147 + 'packages/react-devtools*/**/*.js',
148 ],
149 rules: {
150 'react-internal/no-production-logging': OFF,
@@ -171,6 +168,7 @@ module.exports = {
168 __PROFILE__: true,
169 __UMD__: true,
170 __EXPERIMENTAL__: true,
171 + __VARIANT__: true,
172 trustedTypes: true,
173 },
174 };
package.json
+2
@@ -108,6 +108,8 @@
108 "postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json && node ./scripts/flow/createFlowConfigs.js && node ./scripts/yarn/downloadReactIsForPrettyFormat.js",
109 "debug-test": "cross-env NODE_ENV=development node --inspect-brk node_modules/jest/bin/jest.js --config ./scripts/jest/config.source.js --runInBand",
110 "test": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source.js",
111 + "test-www": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source-www.js",
112 + "test-www-variant": "cross-env NODE_ENV=development VARIANT=true jest --config ./scripts/jest/config.source-www.js",
113 "test-persistent": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source-persistent.js",
114 "debug-test-persistent": "cross-env NODE_ENV=development node --inspect-brk node_modules/jest/bin/jest.js --config ./scripts/jest/config.source-persistent.js --runInBand",
115 "test-prod": "cross-env NODE_ENV=production jest --config ./scripts/jest/config.source.js",
packages/react-dom/src/__tests__/ReactServerRendering-test.js
+3 -1
@@ -14,6 +14,8 @@ let React;
14 let ReactDOMServer;
15 let PropTypes;
16 let ReactCurrentDispatcher;
17 +let enableSuspenseServerRenderer = require('shared/ReactFeatureFlags')
18 + .enableSuspenseServerRenderer;
19
20 function normalizeCodeLocInfo(str) {
21 return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
@@ -686,7 +688,7 @@ describe('ReactDOMServer', () => {
688 expect(markup).toBe('<div></div>');
689 });
690
689 - if (!__EXPERIMENTAL__) {
691 + if (!enableSuspenseServerRenderer) {
692 it('throws for unsupported types on the server', () => {
693 expect(() => {
694 ReactDOMServer.renderToString(<React.Suspense />);
packages/react-reconciler/src/ReactFiber.js
+1 -1
@@ -973,10 +973,10 @@ export function assignFiberPropertiesInDEV(
973 }
974 if (enableUserTimingAPI) {
975 target._debugID = source._debugID;
976 + target._debugIsCurrentlyTiming = source._debugIsCurrentlyTiming;
977 }
978 target._debugSource = source._debugSource;
979 target._debugOwner = source._debugOwner;
979 - target._debugIsCurrentlyTiming = source._debugIsCurrentlyTiming;
980 target._debugNeedsRemount = source._debugNeedsRemount;
981 target._debugHookTypes = source._debugHookTypes;
982 return target;
packages/shared/forks/ReactFeatureFlags.www-dynamic.js new
+32
@@ -0,0 +1,32 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + *
7 + * @flow strict
8 + */
9 +
10 +// In www, these flags are controlled by GKs. Because most GKs have some
11 +// population running in either mode, we should run our tests that way, too,
12 +//
13 +// Use __VARIANT__ to simulate a GK. The tests will be run twice: once
14 +// with the __VARIANT__ set to `true`, and once set to `false`.
15 +
16 +export const deferPassiveEffectCleanupDuringUnmount = __VARIANT__;
17 +export const runAllPassiveEffectDestroysBeforeCreates = __VARIANT__;
18 +export const warnAboutSpreadingKeyToJSX = __VARIANT__;
19 +
20 +// These are already tested in both modes using the build type dimension,
21 +// so we don't need to use __VARIANT__ to get extra coverage.
22 +export const debugRenderPhaseSideEffectsForStrictMode = __DEV__;
23 +export const replayFailedUnitOfWorkWithInvokeGuardedCallback = __DEV__;
24 +
25 +// TODO: These flags are hard-coded to the default values used in open source.
26 +// Update the tests so that they pass in either mode, then set these
27 +// to __VARIANT__.
28 +export const enableTrustedTypesIntegration = false;
29 +export const warnAboutShorthandPropertyCollision = true;
30 +export const disableInputAttributeSyncing = false;
31 +export const disableSchedulerTimeoutBasedOnReactExpirationTime = false;
32 +export const enableModernEventSystem = false;
packages/shared/forks/ReactFeatureFlags.www.js
+5 -2
@@ -9,8 +9,11 @@
9
10 import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
11 import typeof * as ExportsType from './ReactFeatureFlags.www';
12 +import typeof * as DynamicFeatureFlags from './ReactFeatureFlags.www-dynamic';
13
14 // Re-export dynamic flags from the www version.
15 +const dynamicFeatureFlags: DynamicFeatureFlags = require('ReactFeatureFlags');
16 +
17 export const {
18 debugRenderPhaseSideEffectsForStrictMode,
19 deferPassiveEffectCleanupDuringUnmount,
@@ -20,8 +23,9 @@ export const {
23 warnAboutShorthandPropertyCollision,
24 disableSchedulerTimeoutBasedOnReactExpirationTime,
25 warnAboutSpreadingKeyToJSX,
26 + replayFailedUnitOfWorkWithInvokeGuardedCallback,
27 enableModernEventSystem,
24 -} = require('ReactFeatureFlags');
28 +} = dynamicFeatureFlags;
29
30 // On WWW, __EXPERIMENTAL__ is used for a new modern build.
31 // It's not used anywhere in production yet.
@@ -39,7 +43,6 @@ export const enableProfilerCommitHooks = false;
43 export const enableSchedulerTracing = __PROFILE__;
44 export const enableSchedulerDebugging = true;
45
42 -export const replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
46 export const warnAboutDeprecatedLifecycles = true;
47 export const disableLegacyContext = __EXPERIMENTAL__;
48 export const warnAboutStringRefs = false;
scripts/flow/environment.js
+1
@@ -12,6 +12,7 @@
12 declare var __PROFILE__: boolean;
13 declare var __UMD__: boolean;
14 declare var __EXPERIMENTAL__: boolean;
15 +declare var __VARIANT__: boolean;
16
17 declare var __REACT_DEVTOOLS_GLOBAL_HOOK__: any; /*?{
18 inject: ?((stuff: Object) => void)
scripts/jest/config.source-www.js new
+36
@@ -0,0 +1,36 @@
1 +'use strict';
2 +
3 +const baseConfig = require('./config.base');
4 +
5 +const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;
6 +
7 +// Default to building in experimental mode. If the release channel is set via
8 +// an environment variable, then check if it's "experimental".
9 +const __EXPERIMENTAL__ =
10 + typeof RELEASE_CHANNEL === 'string'
11 + ? RELEASE_CHANNEL === 'experimental'
12 + : true;
13 +
14 +const preferredExtension = __EXPERIMENTAL__ ? '.js' : '.stable.js';
15 +
16 +const moduleNameMapper = {};
17 +moduleNameMapper[
18 + '^react$'
19 +] = `<rootDir>/packages/react/index${preferredExtension}`;
20 +moduleNameMapper[
21 + '^react-dom$'
22 +] = `<rootDir>/packages/react-dom/index${preferredExtension}`;
23 +
24 +module.exports = Object.assign({}, baseConfig, {
25 + // Prefer the stable forks for tests.
26 + moduleNameMapper,
27 + modulePathIgnorePatterns: [
28 + ...baseConfig.modulePathIgnorePatterns,
29 + 'packages/react-devtools-shared',
30 + ],
31 + setupFiles: [
32 + ...baseConfig.setupFiles,
33 + require.resolve('./setupHostConfigs.js'),
34 + require.resolve('./setupTests.www.js'),
35 + ],
36 +});
scripts/jest/setupEnvironment.js
+2
@@ -17,6 +17,8 @@ global.__EXPERIMENTAL__ =
17 ? RELEASE_CHANNEL === 'experimental'
18 : true;
19
20 +global.__VARIANT__ = !!process.env.VARIANT;
21 +
22 if (typeof window !== 'undefined') {
23 global.requestIdleCallback = function(callback) {
24 return setTimeout(() => {
scripts/jest/setupTests.www.js new
+23
@@ -0,0 +1,23 @@
1 +'use strict';
2 +
3 +jest.mock('shared/ReactFeatureFlags', () => {
4 + jest.mock(
5 + 'ReactFeatureFlags',
6 + () => jest.requireActual('shared/forks/ReactFeatureFlags.www-dynamic'),
7 + {virtual: true}
8 + );
9 +
10 + const wwwFlags = jest.requireActual('shared/forks/ReactFeatureFlags.www');
11 + const defaultFlags = jest.requireActual('shared/ReactFeatureFlags');
12 +
13 + // TODO: Many tests were written before we started running them against the
14 + // www configuration. Update those tests so that they work against the www
15 + // configuration, too. Then remove these overrides.
16 + wwwFlags.disableLegacyContext = defaultFlags.disableLegacyContext;
17 + wwwFlags.warnAboutUnmockedScheduler = defaultFlags.warnAboutUnmockedScheduler;
18 + wwwFlags.enableUserTimingAPI = defaultFlags.enableUserTimingAPI;
19 + wwwFlags.disableJavaScriptURLs = defaultFlags.disableJavaScriptURLs;
20 + wwwFlags.enableDeprecatedFlareAPI = defaultFlags.enableDeprecatedFlareAPI;
21 +
22 + return wwwFlags;
23 +});
scripts/rollup/build.js
+1
@@ -391,6 +391,7 @@ function getPlugins(
391 __UMD__: isUMDBundle ? 'true' : 'false',
392 'process.env.NODE_ENV': isProduction ? "'production'" : "'development'",
393 __EXPERIMENTAL__,
394 + __VARIANT__: false,
395 }),
396 // The CommonJS plugin *only* exists to pull "art" into "react-art".
397 // I'm going to port "art" to ES modules to avoid this problem.