GeezerVison is showing github.com — make testing builds for React/ReactDOM (#17915) · react/react@3e9251d
File treeExpand file treeCollapse file tree Open diff view settings fixtures/dom/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/dom/src/__tests__/nested-act-test.js +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | let React; | 11 | | -let TestUtils; | | 11 | +let ReactDOM; | 12 | 12 | let TestRenderer; | 13 | 13 |
| 14 | 14 | global.__DEV__ = process.env.NODE_ENV !== 'production'; | 15 | 15 |
| | 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 | | 20 | + | 16 | 21 | expect.extend(require('../toWarnDev')); | 17 | 22 |
| 18 | 23 | describe('unmocked scheduler', () => { | 19 | 24 | beforeEach(() => { | 20 | 25 | jest.resetModules(); | 21 | 26 | React = require('react'); | 22 | | - TestUtils = require('react-dom/test-utils'); | | 27 | + ReactDOM = require('react-dom'); | 23 | 28 | TestRenderer = require('react-test-renderer'); | 24 | 29 | }); | 25 | 30 |
| @@ -33,7 +38,7 @@ describe('unmocked scheduler', () => { | 33 | 38 | } | 34 | 39 | // in legacy mode, this tests whether an act only flushes its own effects | 35 | 40 | TestRenderer.act(() => { | 36 | | - TestUtils.act(() => { | | 41 | + ReactDOM.act(() => { | 37 | 42 | TestRenderer.create(<Effecty />); | 38 | 43 | }); | 39 | 44 | expect(log).toEqual([]); | @@ -42,7 +47,7 @@ describe('unmocked scheduler', () => { | 42 | 47 |
| 43 | 48 | log = []; | 44 | 49 | // for doublechecking, we flip it inside out, and assert on the outermost | 45 | | - TestUtils.act(() => { | | 50 | + ReactDOM.act(() => { | 46 | 51 | TestRenderer.act(() => { | 47 | 52 | TestRenderer.create(<Effecty />); | 48 | 53 | }); | @@ -59,7 +64,7 @@ describe('mocked scheduler', () => { | 59 | 64 | require.requireActual('scheduler/unstable_mock') | 60 | 65 | ); | 61 | 66 | React = require('react'); | 62 | | - TestUtils = require('react-dom/test-utils'); | | 67 | + ReactDOM = require('react-dom'); | 63 | 68 | TestRenderer = require('react-test-renderer'); | 64 | 69 | }); | 65 | 70 |
| @@ -77,7 +82,7 @@ describe('mocked scheduler', () => { | 77 | 82 | } | 78 | 83 | // with a mocked scheduler, this tests whether it flushes all work only on the outermost act | 79 | 84 | TestRenderer.act(() => { | 80 | | - TestUtils.act(() => { | | 85 | + ReactDOM.act(() => { | 81 | 86 | TestRenderer.create(<Effecty />); | 82 | 87 | }); | 83 | 88 | expect(log).toEqual([]); | @@ -86,7 +91,7 @@ describe('mocked scheduler', () => { | 86 | 91 |
| 87 | 92 | log = []; | 88 | 93 | // for doublechecking, we flip it inside out, and assert on the outermost | 89 | | - TestUtils.act(() => { | | 94 | + ReactDOM.act(() => { | 90 | 95 | TestRenderer.act(() => { | 91 | 96 | TestRenderer.create(<Effecty />); | 92 | 97 | }); | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/dom/src/__tests__/wrong-act-test.js +16-13Lines changed: 16 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,13 +12,17 @@ let ReactDOM; | 12 | 12 | let ReactART; | 13 | 13 | let ARTSVGMode; | 14 | 14 | let ARTCurrentMode; | 15 | | -let TestUtils; | 16 | 15 | let TestRenderer; | 17 | 16 | let ARTTest; | 18 | 17 |
| 19 | 18 | global.__DEV__ = process.env.NODE_ENV !== 'production'; | 20 | 19 | global.__EXPERIMENTAL__ = process.env.RELEASE_CHANNEL === 'experimental'; | 21 | 20 |
| | 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 | | 25 | + | 22 | 26 | expect.extend(require('../toWarnDev')); | 23 | 27 |
| 24 | 28 | function App(props) { | @@ -32,7 +36,6 @@ beforeEach(() => { | 32 | 36 | ReactART = require('react-art'); | 33 | 37 | ARTSVGMode = require('art/modes/svg'); | 34 | 38 | ARTCurrentMode = require('art/modes/current'); | 35 | | - TestUtils = require('react-dom/test-utils'); | 36 | 39 | TestRenderer = require('react-test-renderer'); | 37 | 40 |
| 38 | 41 | ARTCurrentMode.setCurrent(ARTSVGMode); | @@ -70,8 +73,8 @@ beforeEach(() => { | 70 | 73 | }); | 71 | 74 |
| 72 | 75 | it("doesn't warn when you use the right act + renderer: dom", () => { | 73 | | - TestUtils.act(() => { | 74 | | - TestUtils.renderIntoDocument(<App />); | | 76 | + ReactDOM.act(() => { | | 77 | + ReactDOM.render(<App />, document.createElement('div')); | 75 | 78 | }); | 76 | 79 | }); | 77 | 80 |
| @@ -86,7 +89,7 @@ it('resets correctly across renderers', () => { | 86 | 89 | React.useEffect(() => {}, []); | 87 | 90 | return null; | 88 | 91 | } | 89 | | - TestUtils.act(() => { | | 92 | + ReactDOM.act(() => { | 90 | 93 | TestRenderer.act(() => {}); | 91 | 94 | expect(() => { | 92 | 95 | TestRenderer.create(<Effecty />); | @@ -99,7 +102,7 @@ it('resets correctly across renderers', () => { | 99 | 102 | it('warns when using the wrong act version - test + dom: render', () => { | 100 | 103 | expect(() => { | 101 | 104 | TestRenderer.act(() => { | 102 | | - TestUtils.renderIntoDocument(<App />); | | 105 | + ReactDOM.render(<App />, document.createElement('div')); | 103 | 106 | }); | 104 | 107 | }).toWarnDev(["It looks like you're using the wrong act()"], { | 105 | 108 | withoutStack: true, | @@ -113,7 +116,7 @@ it('warns when using the wrong act version - test + dom: updates', () => { | 113 | 116 | setCtr = _setCtr; | 114 | 117 | return ctr; | 115 | 118 | } | 116 | | - TestUtils.renderIntoDocument(<Counter />); | | 119 | + ReactDOM.render(<Counter />, document.createElement('div')); | 117 | 120 | expect(() => { | 118 | 121 | TestRenderer.act(() => { | 119 | 122 | setCtr(1); | @@ -123,7 +126,7 @@ it('warns when using the wrong act version - test + dom: updates', () => { | 123 | 126 |
| 124 | 127 | it('warns when using the wrong act version - dom + test: .create()', () => { | 125 | 128 | expect(() => { | 126 | | - TestUtils.act(() => { | | 129 | + ReactDOM.act(() => { | 127 | 130 | TestRenderer.create(<App />); | 128 | 131 | }); | 129 | 132 | }).toWarnDev(["It looks like you're using the wrong act()"], { | @@ -134,7 +137,7 @@ it('warns when using the wrong act version - dom + test: .create()', () => { | 134 | 137 | it('warns when using the wrong act version - dom + test: .update()', () => { | 135 | 138 | const root = TestRenderer.create(<App key="one" />); | 136 | 139 | expect(() => { | 137 | | - TestUtils.act(() => { | | 140 | + ReactDOM.act(() => { | 138 | 141 | root.update(<App key="two" />); | 139 | 142 | }); | 140 | 143 | }).toWarnDev(["It looks like you're using the wrong act()"], { | @@ -151,15 +154,15 @@ it('warns when using the wrong act version - dom + test: updates', () => { | 151 | 154 | } | 152 | 155 | TestRenderer.create(<Counter />); | 153 | 156 | expect(() => { | 154 | | - TestUtils.act(() => { | | 157 | + ReactDOM.act(() => { | 155 | 158 | setCtr(1); | 156 | 159 | }); | 157 | 160 | }).toWarnDev(["It looks like you're using the wrong act()"]); | 158 | 161 | }); | 159 | 162 |
| 160 | 163 | it('does not warn when nesting react-act inside react-dom', () => { | 161 | | - TestUtils.act(() => { | 162 | | - TestUtils.renderIntoDocument(<ARTTest />); | | 164 | + ReactDOM.act(() => { | | 165 | + ReactDOM.render(<ARTTest />, document.createElement('div')); | 163 | 166 | }); | 164 | 167 | }); | 165 | 168 |
| @@ -171,7 +174,7 @@ it('does not warn when nesting react-act inside react-test-renderer', () => { | 171 | 174 |
| 172 | 175 | it("doesn't warn if you use nested acts from different renderers", () => { | 173 | 176 | TestRenderer.act(() => { | 174 | | - TestUtils.act(() => { | | 177 | + ReactDOM.act(() => { | 175 | 178 | TestRenderer.create(<App />); | 176 | 179 | }); | 177 | 180 | }); | |
Collapse file Copy file name to clipboard +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +function checkDCE() { | | 4 | + /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */ | | 5 | + if ( | | 6 | + typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined' || | | 7 | + typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE !== 'function' | | 8 | + ) { | | 9 | + return; | | 10 | + } | | 11 | + if (process.env.NODE_ENV !== 'production') { | | 12 | + // This branch is unreachable because this function is only called | | 13 | + // in production, but the condition is true only in development. | | 14 | + // Therefore if the branch is still here, dead code elimination wasn't | | 15 | + // properly applied. | | 16 | + // Don't change the message. React DevTools relies on it. Also make sure | | 17 | + // this message doesn't occur elsewhere in this function, or it will cause | | 18 | + // a false positive. | | 19 | + throw new Error('^_^'); | | 20 | + } | | 21 | + try { | | 22 | + // Verify that the code above has been dead code eliminated (DCE'd). | | 23 | + __REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE(checkDCE); | | 24 | + } catch (err) { | | 25 | + // DevTools shouldn't crash React, no matter what. | | 26 | + // We should still report in case we break this code. | | 27 | + console.error(err); | | 28 | + } | | 29 | +} | | 30 | + | | 31 | +if (process.env.NODE_ENV === 'production') { | | 32 | + // DCE check should happen before ReactDOM bundle executes so that | | 33 | + // DevTools can report bad minification during injection. | | 34 | + checkDCE(); | | 35 | + module.exports = require('./cjs/react-dom-testing.production.min.js'); | | 36 | +} else { | | 37 | + module.exports = require('./cjs/react-dom-testing.development.js'); | | 38 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -722,6 +722,7 @@ function runActTests(label, render, unmount, rerender) { | 722 | 722 |
| 723 | 723 | describe('suspense', () => { | 724 | 724 | if (__DEV__ && __EXPERIMENTAL__) { | | 725 | + // todo - remove __DEV__ check once we start using testing builds | 725 | 726 | it('triggers fallbacks if available', async () => { | 726 | 727 | let resolved = false; | 727 | 728 | let resolve; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOM.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { | 35 | 35 | attemptUserBlockingHydration, | 36 | 36 | attemptContinuousHydration, | 37 | 37 | attemptHydrationAtCurrentPriority, | | 38 | + act, | 38 | 39 | } from 'react-reconciler/inline.dom'; | 39 | 40 | import {createPortal as createPortalImpl} from 'shared/ReactPortal'; | 40 | 41 | import {canUseDOM} from 'shared/ExecutionEnvironment'; | @@ -58,6 +59,7 @@ import { | 58 | 59 | disableUnstableCreatePortal, | 59 | 60 | disableUnstableRenderSubtreeIntoContainer, | 60 | 61 | warnUnstableRenderSubtreeIntoContainer, | | 62 | + isTestEnvironment, | 61 | 63 | } from 'shared/ReactFeatureFlags'; | 62 | 64 |
| 63 | 65 | import { | @@ -251,4 +253,8 @@ if (__DEV__) { | 251 | 253 | } | 252 | 254 | } | 253 | 255 |
| | 256 | +if (isTestEnvironment) { | | 257 | + ReactDOM.act = act; | | 258 | +} | | 259 | + | 254 | 260 | export default ReactDOM; |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +'use strict'; | | 11 | + | | 12 | +const ReactDOM = require('./src/client/ReactDOM'); | | 13 | + | | 14 | +// TODO: decide on the top-level export form. | | 15 | +// This is hacky but makes it work with both Rollup and Jest. | | 16 | +module.exports = ReactDOM.default || ReactDOM; |
|