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 256d78d commit fe3bb48
Show file tree
Hide file tree
Showing 20 changed files with 17 additions and 88 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ module.exports = {
__PROFILE__: true,
__UMD__: true,
__EXPERIMENTAL__: true,
__TEST__: true,
trustedTypes: true,
},
};
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
3 changes: 1 addition & 2 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
disableUnstableCreatePortal,
disableUnstableRenderSubtreeIntoContainer,
warnUnstableRenderSubtreeIntoContainer,
isTestEnvironment,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -253,7 +252,7 @@ if (__DEV__) {
}
}

if (isTestEnvironment) {
if (__TEST__) {
ReactDOM.act = act;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ import {
findHostInstancesForRefresh,
} from './ReactFiberHotReloading';

// used by isTestEnvironment builds
// used by __TEST__ builds
import enqueueTask from 'shared/enqueueTask';
import * as Scheduler from 'scheduler';
// end isTestEnvironment imports
// end __TEST__ imports

type OpaqueRoot = FiberRoot;

Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = false;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = false;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = false;

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
1 change: 0 additions & 1 deletion packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = true; // this should probably *never* change

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const disableUnstableRenderSubtreeIntoContainer = false;
export const warnUnstableRenderSubtreeIntoContainer = false;
export const disableUnstableCreatePortal = false;
export const deferPassiveEffectCleanupDuringUnmount = false;
export const isTestEnvironment = true; // this should probably *never* change

// Only used in www builds.
export function addUserTimingListener() {
Expand Down
61 changes: 0 additions & 61 deletions packages/shared/forks/ReactFeatureFlags.testing.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/shared/forks/ReactFeatureFlags.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ export const warnUnstableRenderSubtreeIntoContainer = false;

export const disableUnstableCreatePortal = false;

export const isTestEnvironment = false;

// 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;
Expand Down
1 change: 1 addition & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/* eslint-disable */

declare var __TEST__: boolean;
declare var __PROFILE__: boolean;
declare var __UMD__: boolean;
declare var __EXPERIMENTAL__: boolean;
Expand Down
1 change: 1 addition & 0 deletions scripts/jest/setupEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (NODE_ENV !== 'development' && NODE_ENV !== 'production') {
global.__DEV__ = NODE_ENV === 'development';
global.__PROFILE__ = NODE_ENV === 'development';
global.__UMD__ = false;
global.__TEST__ = true;

const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;

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
2 changes: 2 additions & 0 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ function getPlugins(
bundleType === RN_FB_DEV ||
bundleType === RN_FB_PROD ||
bundleType === RN_FB_PROFILING;
const isTestBuild = entry === 'react-dom/testing';
const shouldStayReadable = isFBBundle || isRNBundle || forcePrettyOutput;
return [
// Extract error codes from invariant() messages into a file.
Expand Down Expand Up @@ -391,6 +392,7 @@ function getPlugins(
__DEV__: isProduction ? 'false' : 'true',
__PROFILE__: isProfiling || !isProduction ? 'true' : 'false',
__UMD__: isUMDBundle ? 'true' : 'false',
__TEST__: isTestBuild ? 'true' : 'false',
'process.env.NODE_ENV': isProduction ? "'production'" : "'development'",
__EXPERIMENTAL__,
}),
Expand Down
6 changes: 1 addition & 5 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 @@ -106,10 +106,6 @@ const forks = Object.freeze({
return 'shared/forks/ReactFeatureFlags.test-renderer.www.js';
}
return 'shared/forks/ReactFeatureFlags.test-renderer.js';
case 'react-dom/testing':
return 'shared/forks/ReactFeatureFlags.testing.js';
case 'react/testing':
return 'shared/forks/ReactFeatureFlags.testing.js';
default:
switch (bundleType) {
case FB_WWW_DEV:
Expand Down

0 comments on commit fe3bb48

Please sign in to comment.