Skip to content

Commit

Permalink
Use testing builds for our own tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
threepointone committed Feb 6, 2020
1 parent 9e158c0 commit 7bf2df2
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 55 deletions.
5 changes: 1 addition & 4 deletions fixtures/dom/src/__tests__/nested-act-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ let TestRenderer;

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

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

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

Expand Down
5 changes: 1 addition & 4 deletions fixtures/dom/src/__tests__/wrong-act-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ let ARTTest;
global.__DEV__ = process.env.NODE_ENV !== 'production';
global.__EXPERIMENTAL__ = process.env.RELEASE_CHANNEL === 'experimental';

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

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

Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"README.md",
"build-info.json",
"index.js",
"testing.js",
"profiling.js",
"server.js",
"server.browser.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let React;
let ReactDOM;
let Suspense;
let ReactCache;
let ReactTestUtils;
let Scheduler;
let TextResource;
let act;
Expand All @@ -26,9 +25,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactCache = require('react-cache');
ReactTestUtils = require('react-dom/test-utils');
Scheduler = require('scheduler');
act = ReactTestUtils.act;
act = ReactDOM.act;
Suspense = React.Suspense;
container = document.createElement('div');
document.body.appendChild(container);
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/forks/ReactFeatureFlags.readonly.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
// It lets us determine whether we're running in Fire mode without making tests internal.
const ReactFeatureFlags = require('../ReactFeatureFlags');
// Forbid writes because this wouldn't work with bundle tests.
module.exports = Object.freeze({...ReactFeatureFlags});
module.exports = Object.freeze({...ReactFeatureFlags, isTestEnvironment: true});
80 changes: 40 additions & 40 deletions packages/shared/forks/ReactFeatureFlags.testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@
* @flow
*/

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

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

export const debugRenderPhaseSideEffectsForStrictMode = false;
export const enableUserTimingAPI = __DEV__;
export const warnAboutDeprecatedLifecycles = true;
export const replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
export const enableProfilerTimer = __PROFILE__;
export const enableSchedulerTracing = __PROFILE__;
export const enableSuspenseServerRenderer = false;
export const enableSelectiveHydration = false;
export const enableChunksAPI = false;
export const disableJavaScriptURLs = false;
export const disableInputAttributeSyncing = false;
export const exposeConcurrentModeAPIs = __EXPERIMENTAL__;
export const warnAboutShorthandPropertyCollision = false;
export const enableSchedulerDebugging = false;
export const enableDeprecatedFlareAPI = false;
export const enableFundamentalAPI = false;
export const enableScopeAPI = false;
export const enableJSXTransformAPI = false;
export const warnAboutUnmockedScheduler = false;
export const flushSuspenseFallbacksInTests = true;
export const enableSuspenseCallback = false;
export const warnAboutDefaultPropsOnFunctionComponents = false;
export const warnAboutStringRefs = false;
export const disableLegacyContext = false;
export const disableSchedulerTimeoutBasedOnReactExpirationTime = false;
export const enableTrainModelFix = true;
export const enableTrustedTypesIntegration = false;
export const enableNativeTargetAsInstance = false;
export const disableCreateFactory = false;
export const disableTextareaChildren = false;
export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = true;
export {
enableUserTimingAPI,
debugRenderPhaseSideEffectsForStrictMode,
replayFailedUnitOfWorkWithInvokeGuardedCallback,
warnAboutDeprecatedLifecycles,
enableProfilerTimer,
enableSchedulerTracing,
enableSuspenseServerRenderer,
enableSelectiveHydration,
enableChunksAPI,
enableSchedulerDebugging,
disableJavaScriptURLs,
exposeConcurrentModeAPIs,
warnAboutShorthandPropertyCollision,
enableDeprecatedFlareAPI,
enableFundamentalAPI,
enableScopeAPI,
enableJSXTransformAPI,
warnAboutUnmockedScheduler,
flushSuspenseFallbacksInTests,
enableSuspenseCallback,
warnAboutDefaultPropsOnFunctionComponents,
disableSchedulerTimeoutBasedOnReactExpirationTime,
enableTrainModelFix,
enableTrustedTypesIntegration,
enableNativeTargetAsInstance,
deferPassiveEffectCleanupDuringUnmount,
disableInputAttributeSyncing,
warnAboutStringRefs,
disableLegacyContext,
disableCreateFactory,
disableTextareaChildren,
disableUnstableRenderSubtreeIntoContainer,
warnUnstableRenderSubtreeIntoContainer,
disableUnstableCreatePortal,
addUserTimingListener,
} from 'shared/ReactFeatureFlags';

// Only used in www builds.
export function addUserTimingListener() {
invariant(false, 'Not implemented.');
}
export const isTestEnvironment = true;

// Flow magic to verify the exports of this file match the original version.
// eslint-disable-next-line no-unused-vars
Expand Down
64 changes: 64 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.testing.www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// keep in sync with forks/ReactFeatureFlags.testing.js
// only isTestEnvironment is different, and true

import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.www';

// Re-export dynamic flags from the www version.
export const {
debugRenderPhaseSideEffectsForStrictMode,
disableInputAttributeSyncing,
enableTrustedTypesIntegration,
deferPassiveEffectCleanupDuringUnmount,
} = require('ReactFeatureFlags');

export {
enableUserTimingAPI,
replayFailedUnitOfWorkWithInvokeGuardedCallback,
warnAboutDeprecatedLifecycles,
enableProfilerTimer,
enableSchedulerTracing,
enableSuspenseServerRenderer,
enableSelectiveHydration,
enableChunksAPI,
enableSchedulerDebugging,
disableJavaScriptURLs,
exposeConcurrentModeAPIs,
warnAboutShorthandPropertyCollision,
enableDeprecatedFlareAPI,
enableFundamentalAPI,
enableScopeAPI,
enableJSXTransformAPI,
warnAboutUnmockedScheduler,
flushSuspenseFallbacksInTests,
enableSuspenseCallback,
warnAboutDefaultPropsOnFunctionComponents,
disableSchedulerTimeoutBasedOnReactExpirationTime,
enableTrainModelFix,
enableNativeTargetAsInstance,
warnAboutStringRefs,
disableLegacyContext,
disableCreateFactory,
disableTextareaChildren,
disableUnstableRenderSubtreeIntoContainer,
warnUnstableRenderSubtreeIntoContainer,
disableUnstableCreatePortal,
addUserTimingListener,
} from 'shared/ReactFeatureFlags.www';

export const isTestEnvironment = true;

// Flow magic to verify the exports of this file match the original version.
// eslint-disable-next-line no-unused-vars
type Check<_X, Y: _X, X: Y = _X> = null;
// eslint-disable-next-line no-unused-expressions
(null: Check<FeatureFlagsShimType, FeatureFlagsType>);
3 changes: 3 additions & 0 deletions scripts/jest/config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module.exports = {
haste: {
hasteImplModulePath: require.resolve('./noHaste.js'),
},
moduleNameMapper: {
'^shared/ReactFeatureFlags': `<rootDir>/packages/shared/forks/ReactFeatureFlags.testing`,
},
modulePathIgnorePatterns: [
'<rootDir>/scripts/rollup/shims/',
'<rootDir>/scripts/bench/',
Expand Down
2 changes: 2 additions & 0 deletions scripts/jest/setupHostConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ inlinedHostConfigs.forEach(rendererInfo => {
}
});

jest.mock('react-dom', () => require.requireActual('react-dom/testing'));

// Make it possible to import this module inside
// the React package itself.
jest.mock('shared/ReactSharedInternals', () =>
Expand Down
2 changes: 2 additions & 0 deletions scripts/jest/setupTests.build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

jest.mock('react-dom', () => require.requireActual(`react-dom/testing`));

jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));
jest.mock('scheduler/src/SchedulerHostConfig', () =>
require.requireActual('scheduler/src/forks/SchedulerHostConfig.mock.js')
Expand Down
10 changes: 7 additions & 3 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const forks = Object.freeze({
// Without this fork, importing `shared/ReactSharedInternals` inside
// the `react` package itself would not work due to a cyclical dependency.
'shared/ReactSharedInternals': (bundleType, entry, dependencies) => {
if (entry === 'react' || entry === 'react/testing') {
if (entry === 'react') {
return 'react/src/ReactSharedInternals';
}
if (dependencies.indexOf('react') === -1) {
Expand Down Expand Up @@ -107,8 +107,12 @@ const forks = Object.freeze({
}
return 'shared/forks/ReactFeatureFlags.test-renderer.js';
case 'react-dom/testing':
return 'shared/forks/ReactFeatureFlags.testing.js';
case 'react/testing':
switch (bundleType) {
case FB_WWW_DEV:
case FB_WWW_PROD:
case FB_WWW_PROFILING:
return 'shared/forks/ReactFeatureFlags.testing.www.js';
}
return 'shared/forks/ReactFeatureFlags.testing.js';
default:
switch (bundleType) {
Expand Down

0 comments on commit 7bf2df2

Please sign in to comment.