Skip to content

Commit 7bf2df2

Browse files
committed
Use testing builds for our own tests
Following up from facebook#17915 where we generated testing builds for `react-dom`, this PR uses those builds for our own tests. - fixes `ReactFeatureFlags.testing` to use all the default flags - changes `ReactFeatureFlags.readonly` to return `isTestEnvironment: true` (this might not strictly be needed) - with jest, mocks `react-dom` with the testing version, both for source and builds - uses `ReactDOM.act` in one of these tests to verify it actually works
1 parent 9e158c0 commit 7bf2df2

File tree

11 files changed

+123
-55
lines changed

11 files changed

+123
-55
lines changed

fixtures/dom/src/__tests__/nested-act-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ let TestRenderer;
1313

1414
global.__DEV__ = process.env.NODE_ENV !== 'production';
1515

16-
jest.mock('react-dom', () =>
17-
require.requireActual('react-dom/cjs/react-dom-testing.development.js')
18-
);
19-
// we'll replace the above with react/testing and react-dom/testing right before the next minor
16+
jest.mock('react-dom', () => require.requireActual('react-dom/testing.js'));
2017

2118
expect.extend(require('../toWarnDev'));
2219

fixtures/dom/src/__tests__/wrong-act-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ let ARTTest;
1818
global.__DEV__ = process.env.NODE_ENV !== 'production';
1919
global.__EXPERIMENTAL__ = process.env.RELEASE_CHANNEL === 'experimental';
2020

21-
jest.mock('react-dom', () =>
22-
require.requireActual('react-dom/cjs/react-dom-testing.development.js')
23-
);
24-
// we'll replace the above with react/testing and react-dom/testing right before the next minor
21+
jest.mock('react-dom', () => require.requireActual('react-dom/testing.js'));
2522

2623
expect.extend(require('../toWarnDev'));
2724

packages/react-dom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"README.md",
3131
"build-info.json",
3232
"index.js",
33+
"testing.js",
3334
"profiling.js",
3435
"server.js",
3536
"server.browser.js",

packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ let React;
1313
let ReactDOM;
1414
let Suspense;
1515
let ReactCache;
16-
let ReactTestUtils;
1716
let Scheduler;
1817
let TextResource;
1918
let act;
@@ -26,9 +25,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
2625
React = require('react');
2726
ReactDOM = require('react-dom');
2827
ReactCache = require('react-cache');
29-
ReactTestUtils = require('react-dom/test-utils');
3028
Scheduler = require('scheduler');
31-
act = ReactTestUtils.act;
29+
act = ReactDOM.act;
3230
Suspense = React.Suspense;
3331
container = document.createElement('div');
3432
document.body.appendChild(container);

packages/shared/forks/ReactFeatureFlags.readonly.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
// It lets us determine whether we're running in Fire mode without making tests internal.
1010
const ReactFeatureFlags = require('../ReactFeatureFlags');
1111
// Forbid writes because this wouldn't work with bundle tests.
12-
module.exports = Object.freeze({...ReactFeatureFlags});
12+
module.exports = Object.freeze({...ReactFeatureFlags, isTestEnvironment: true});

packages/shared/forks/ReactFeatureFlags.testing.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@
77
* @flow
88
*/
99

10-
import invariant from 'shared/invariant';
10+
// keep in sync with ReactFeatureFlags.js
11+
// only isTestEnvironment is different, and true
1112

1213
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
1314
import typeof * as PersistentFeatureFlagsType from './ReactFeatureFlags.persistent';
1415

15-
export const debugRenderPhaseSideEffectsForStrictMode = false;
16-
export const enableUserTimingAPI = __DEV__;
17-
export const warnAboutDeprecatedLifecycles = true;
18-
export const replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
19-
export const enableProfilerTimer = __PROFILE__;
20-
export const enableSchedulerTracing = __PROFILE__;
21-
export const enableSuspenseServerRenderer = false;
22-
export const enableSelectiveHydration = false;
23-
export const enableChunksAPI = false;
24-
export const disableJavaScriptURLs = false;
25-
export const disableInputAttributeSyncing = false;
26-
export const exposeConcurrentModeAPIs = __EXPERIMENTAL__;
27-
export const warnAboutShorthandPropertyCollision = false;
28-
export const enableSchedulerDebugging = false;
29-
export const enableDeprecatedFlareAPI = false;
30-
export const enableFundamentalAPI = false;
31-
export const enableScopeAPI = false;
32-
export const enableJSXTransformAPI = false;
33-
export const warnAboutUnmockedScheduler = false;
34-
export const flushSuspenseFallbacksInTests = true;
35-
export const enableSuspenseCallback = false;
36-
export const warnAboutDefaultPropsOnFunctionComponents = false;
37-
export const warnAboutStringRefs = false;
38-
export const disableLegacyContext = false;
39-
export const disableSchedulerTimeoutBasedOnReactExpirationTime = false;
40-
export const enableTrainModelFix = true;
41-
export const enableTrustedTypesIntegration = false;
42-
export const enableNativeTargetAsInstance = false;
43-
export const disableCreateFactory = false;
44-
export const disableTextareaChildren = false;
45-
export const disableUnstableRenderSubtreeIntoContainer = false;
46-
export const warnUnstableRenderSubtreeIntoContainer = false;
47-
export const disableUnstableCreatePortal = false;
48-
export const deferPassiveEffectCleanupDuringUnmount = false;
49-
export const isTestEnvironment = true;
16+
export {
17+
enableUserTimingAPI,
18+
debugRenderPhaseSideEffectsForStrictMode,
19+
replayFailedUnitOfWorkWithInvokeGuardedCallback,
20+
warnAboutDeprecatedLifecycles,
21+
enableProfilerTimer,
22+
enableSchedulerTracing,
23+
enableSuspenseServerRenderer,
24+
enableSelectiveHydration,
25+
enableChunksAPI,
26+
enableSchedulerDebugging,
27+
disableJavaScriptURLs,
28+
exposeConcurrentModeAPIs,
29+
warnAboutShorthandPropertyCollision,
30+
enableDeprecatedFlareAPI,
31+
enableFundamentalAPI,
32+
enableScopeAPI,
33+
enableJSXTransformAPI,
34+
warnAboutUnmockedScheduler,
35+
flushSuspenseFallbacksInTests,
36+
enableSuspenseCallback,
37+
warnAboutDefaultPropsOnFunctionComponents,
38+
disableSchedulerTimeoutBasedOnReactExpirationTime,
39+
enableTrainModelFix,
40+
enableTrustedTypesIntegration,
41+
enableNativeTargetAsInstance,
42+
deferPassiveEffectCleanupDuringUnmount,
43+
disableInputAttributeSyncing,
44+
warnAboutStringRefs,
45+
disableLegacyContext,
46+
disableCreateFactory,
47+
disableTextareaChildren,
48+
disableUnstableRenderSubtreeIntoContainer,
49+
warnUnstableRenderSubtreeIntoContainer,
50+
disableUnstableCreatePortal,
51+
addUserTimingListener,
52+
} from 'shared/ReactFeatureFlags';
5053

51-
// Only used in www builds.
52-
export function addUserTimingListener() {
53-
invariant(false, 'Not implemented.');
54-
}
54+
export const isTestEnvironment = true;
5555

5656
// Flow magic to verify the exports of this file match the original version.
5757
// eslint-disable-next-line no-unused-vars
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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
8+
*/
9+
10+
// keep in sync with forks/ReactFeatureFlags.testing.js
11+
// only isTestEnvironment is different, and true
12+
13+
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
14+
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.www';
15+
16+
// Re-export dynamic flags from the www version.
17+
export const {
18+
debugRenderPhaseSideEffectsForStrictMode,
19+
disableInputAttributeSyncing,
20+
enableTrustedTypesIntegration,
21+
deferPassiveEffectCleanupDuringUnmount,
22+
} = require('ReactFeatureFlags');
23+
24+
export {
25+
enableUserTimingAPI,
26+
replayFailedUnitOfWorkWithInvokeGuardedCallback,
27+
warnAboutDeprecatedLifecycles,
28+
enableProfilerTimer,
29+
enableSchedulerTracing,
30+
enableSuspenseServerRenderer,
31+
enableSelectiveHydration,
32+
enableChunksAPI,
33+
enableSchedulerDebugging,
34+
disableJavaScriptURLs,
35+
exposeConcurrentModeAPIs,
36+
warnAboutShorthandPropertyCollision,
37+
enableDeprecatedFlareAPI,
38+
enableFundamentalAPI,
39+
enableScopeAPI,
40+
enableJSXTransformAPI,
41+
warnAboutUnmockedScheduler,
42+
flushSuspenseFallbacksInTests,
43+
enableSuspenseCallback,
44+
warnAboutDefaultPropsOnFunctionComponents,
45+
disableSchedulerTimeoutBasedOnReactExpirationTime,
46+
enableTrainModelFix,
47+
enableNativeTargetAsInstance,
48+
warnAboutStringRefs,
49+
disableLegacyContext,
50+
disableCreateFactory,
51+
disableTextareaChildren,
52+
disableUnstableRenderSubtreeIntoContainer,
53+
warnUnstableRenderSubtreeIntoContainer,
54+
disableUnstableCreatePortal,
55+
addUserTimingListener,
56+
} from 'shared/ReactFeatureFlags.www';
57+
58+
export const isTestEnvironment = true;
59+
60+
// Flow magic to verify the exports of this file match the original version.
61+
// eslint-disable-next-line no-unused-vars
62+
type Check<_X, Y: _X, X: Y = _X> = null;
63+
// eslint-disable-next-line no-unused-expressions
64+
(null: Check<FeatureFlagsShimType, FeatureFlagsType>);

scripts/jest/config.base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module.exports = {
44
haste: {
55
hasteImplModulePath: require.resolve('./noHaste.js'),
66
},
7+
moduleNameMapper: {
8+
'^shared/ReactFeatureFlags': `<rootDir>/packages/shared/forks/ReactFeatureFlags.testing`,
9+
},
710
modulePathIgnorePatterns: [
811
'<rootDir>/scripts/rollup/shims/',
912
'<rootDir>/scripts/bench/',

scripts/jest/setupHostConfigs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ inlinedHostConfigs.forEach(rendererInfo => {
177177
}
178178
});
179179

180+
jest.mock('react-dom', () => require.requireActual('react-dom/testing'));
181+
180182
// Make it possible to import this module inside
181183
// the React package itself.
182184
jest.mock('shared/ReactSharedInternals', () =>

scripts/jest/setupTests.build.js

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

3+
jest.mock('react-dom', () => require.requireActual(`react-dom/testing`));
4+
35
jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));
46
jest.mock('scheduler/src/SchedulerHostConfig', () =>
57
require.requireActual('scheduler/src/forks/SchedulerHostConfig.mock.js')

scripts/rollup/forks.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const forks = Object.freeze({
4646
// Without this fork, importing `shared/ReactSharedInternals` inside
4747
// the `react` package itself would not work due to a cyclical dependency.
4848
'shared/ReactSharedInternals': (bundleType, entry, dependencies) => {
49-
if (entry === 'react' || entry === 'react/testing') {
49+
if (entry === 'react') {
5050
return 'react/src/ReactSharedInternals';
5151
}
5252
if (dependencies.indexOf('react') === -1) {
@@ -107,8 +107,12 @@ const forks = Object.freeze({
107107
}
108108
return 'shared/forks/ReactFeatureFlags.test-renderer.js';
109109
case 'react-dom/testing':
110-
return 'shared/forks/ReactFeatureFlags.testing.js';
111-
case 'react/testing':
110+
switch (bundleType) {
111+
case FB_WWW_DEV:
112+
case FB_WWW_PROD:
113+
case FB_WWW_PROFILING:
114+
return 'shared/forks/ReactFeatureFlags.testing.www.js';
115+
}
112116
return 'shared/forks/ReactFeatureFlags.testing.js';
113117
default:
114118
switch (bundleType) {

0 commit comments

Comments
 (0)