Skip to content

Commit dcfd7c0

Browse files
committed
make testing builds for React/ReactDOM
This PR introduces UMD_TESTING, NODE_TESTING, and FB_WWW_TESTING build types for `react` and `react-dom`. - changes infra to generate these builds - exports act on ReactDOM in these testing builds - uses the new test builds in fixtures/dom In the next PR - - I'll use the new builds for all our own tests - I'll replace usages of TestUtils.act with ReactDOM.act.
1 parent 434770c commit dcfd7c0

File tree

18 files changed

+466
-501
lines changed

18 files changed

+466
-501
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ module.exports = {
168168
__PROFILE__: true,
169169
__UMD__: true,
170170
__EXPERIMENTAL__: true,
171+
__TEST__: true,
171172
trustedTypes: true,
172173
},
173174
};

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
*/
99

1010
let React;
11-
let TestUtils;
11+
let ReactDOM;
1212
let TestRenderer;
1313

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

16+
jest.mock('react', () => require.requireActual('react/cjs/react.testing.js'));
17+
jest.mock('react-dom', () =>
18+
require.requireActual('react-dom/cjs/react-dom.testing.js')
19+
);
20+
// we'll replace the above with react/testing and react-dom/testing right before the next minor
21+
1622
expect.extend(require('../toWarnDev'));
1723

1824
describe('unmocked scheduler', () => {
1925
beforeEach(() => {
2026
jest.resetModules();
2127
React = require('react');
22-
TestUtils = require('react-dom/test-utils');
28+
ReactDOM = require('react-dom');
2329
TestRenderer = require('react-test-renderer');
2430
});
2531

@@ -33,7 +39,7 @@ describe('unmocked scheduler', () => {
3339
}
3440
// in legacy mode, this tests whether an act only flushes its own effects
3541
TestRenderer.act(() => {
36-
TestUtils.act(() => {
42+
ReactDOM.act(() => {
3743
TestRenderer.create(<Effecty />);
3844
});
3945
expect(log).toEqual([]);
@@ -42,7 +48,7 @@ describe('unmocked scheduler', () => {
4248

4349
log = [];
4450
// for doublechecking, we flip it inside out, and assert on the outermost
45-
TestUtils.act(() => {
51+
ReactDOM.act(() => {
4652
TestRenderer.act(() => {
4753
TestRenderer.create(<Effecty />);
4854
});
@@ -59,7 +65,7 @@ describe('mocked scheduler', () => {
5965
require.requireActual('scheduler/unstable_mock')
6066
);
6167
React = require('react');
62-
TestUtils = require('react-dom/test-utils');
68+
ReactDOM = require('react-dom');
6369
TestRenderer = require('react-test-renderer');
6470
});
6571

@@ -77,7 +83,7 @@ describe('mocked scheduler', () => {
7783
}
7884
// with a mocked scheduler, this tests whether it flushes all work only on the outermost act
7985
TestRenderer.act(() => {
80-
TestUtils.act(() => {
86+
ReactDOM.act(() => {
8187
TestRenderer.create(<Effecty />);
8288
});
8389
expect(log).toEqual([]);
@@ -86,7 +92,7 @@ describe('mocked scheduler', () => {
8692

8793
log = [];
8894
// for doublechecking, we flip it inside out, and assert on the outermost
89-
TestUtils.act(() => {
95+
ReactDOM.act(() => {
9096
TestRenderer.act(() => {
9197
TestRenderer.create(<Effecty />);
9298
});

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ let ReactDOM;
1212
let ReactART;
1313
let ARTSVGMode;
1414
let ARTCurrentMode;
15-
let TestUtils;
1615
let TestRenderer;
1716
let ARTTest;
1817

1918
global.__DEV__ = process.env.NODE_ENV !== 'production';
2019
global.__EXPERIMENTAL__ = process.env.RELEASE_CHANNEL === 'experimental';
2120

21+
jest.mock('react', () => require.requireActual('react/cjs/react.testing.js'));
22+
jest.mock('react-dom', () =>
23+
require.requireActual('react-dom/cjs/react-dom.testing.js')
24+
);
25+
// we'll replace the above with react/testing and react-dom/testing right before the next minor
26+
2227
expect.extend(require('../toWarnDev'));
2328

2429
function App(props) {
@@ -32,7 +37,6 @@ beforeEach(() => {
3237
ReactART = require('react-art');
3338
ARTSVGMode = require('art/modes/svg');
3439
ARTCurrentMode = require('art/modes/current');
35-
TestUtils = require('react-dom/test-utils');
3640
TestRenderer = require('react-test-renderer');
3741

3842
ARTCurrentMode.setCurrent(ARTSVGMode);
@@ -70,8 +74,8 @@ beforeEach(() => {
7074
});
7175

7276
it("doesn't warn when you use the right act + renderer: dom", () => {
73-
TestUtils.act(() => {
74-
TestUtils.renderIntoDocument(<App />);
77+
ReactDOM.act(() => {
78+
ReactDOM.render(<App />, document.createElement('div'));
7579
});
7680
});
7781

@@ -86,7 +90,7 @@ it('resets correctly across renderers', () => {
8690
React.useEffect(() => {}, []);
8791
return null;
8892
}
89-
TestUtils.act(() => {
93+
ReactDOM.act(() => {
9094
TestRenderer.act(() => {});
9195
expect(() => {
9296
TestRenderer.create(<Effecty />);
@@ -99,7 +103,7 @@ it('resets correctly across renderers', () => {
99103
it('warns when using the wrong act version - test + dom: render', () => {
100104
expect(() => {
101105
TestRenderer.act(() => {
102-
TestUtils.renderIntoDocument(<App />);
106+
ReactDOM.render(<App />, document.createElement('div'));
103107
});
104108
}).toWarnDev(["It looks like you're using the wrong act()"], {
105109
withoutStack: true,
@@ -113,7 +117,7 @@ it('warns when using the wrong act version - test + dom: updates', () => {
113117
setCtr = _setCtr;
114118
return ctr;
115119
}
116-
TestUtils.renderIntoDocument(<Counter />);
120+
ReactDOM.render(<Counter />, document.createElement('div'));
117121
expect(() => {
118122
TestRenderer.act(() => {
119123
setCtr(1);
@@ -123,7 +127,7 @@ it('warns when using the wrong act version - test + dom: updates', () => {
123127

124128
it('warns when using the wrong act version - dom + test: .create()', () => {
125129
expect(() => {
126-
TestUtils.act(() => {
130+
ReactDOM.act(() => {
127131
TestRenderer.create(<App />);
128132
});
129133
}).toWarnDev(["It looks like you're using the wrong act()"], {
@@ -134,7 +138,7 @@ it('warns when using the wrong act version - dom + test: .create()', () => {
134138
it('warns when using the wrong act version - dom + test: .update()', () => {
135139
const root = TestRenderer.create(<App key="one" />);
136140
expect(() => {
137-
TestUtils.act(() => {
141+
ReactDOM.act(() => {
138142
root.update(<App key="two" />);
139143
});
140144
}).toWarnDev(["It looks like you're using the wrong act()"], {
@@ -151,15 +155,15 @@ it('warns when using the wrong act version - dom + test: updates', () => {
151155
}
152156
TestRenderer.create(<Counter />);
153157
expect(() => {
154-
TestUtils.act(() => {
158+
ReactDOM.act(() => {
155159
setCtr(1);
156160
});
157161
}).toWarnDev(["It looks like you're using the wrong act()"]);
158162
});
159163

160164
it('does not warn when nesting react-act inside react-dom', () => {
161-
TestUtils.act(() => {
162-
TestUtils.renderIntoDocument(<ARTTest />);
165+
ReactDOM.act(() => {
166+
ReactDOM.render(<ARTTest />, document.createElement('div'));
163167
});
164168
});
165169

@@ -171,7 +175,7 @@ it('does not warn when nesting react-act inside react-test-renderer', () => {
171175

172176
it("doesn't warn if you use nested acts from different renderers", () => {
173177
TestRenderer.act(() => {
174-
TestUtils.act(() => {
178+
ReactDOM.act(() => {
175179
TestRenderer.create(<App />);
176180
});
177181
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ function runActTests(label, render, unmount, rerender) {
722722

723723
describe('suspense', () => {
724724
if (__DEV__ && __EXPERIMENTAL__) {
725+
// todo - remove __DEV__ check once we start using testing builds
725726
it('triggers fallbacks if available', async () => {
726727
let resolved = false;
727728
let resolve;

packages/react-dom/src/client/ReactDOM.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
attemptUserBlockingHydration,
3636
attemptContinuousHydration,
3737
attemptHydrationAtCurrentPriority,
38+
act,
3839
} from 'react-reconciler/inline.dom';
3940
import {createPortal as createPortalImpl} from 'shared/ReactPortal';
4041
import {canUseDOM} from 'shared/ExecutionEnvironment';
@@ -249,4 +250,8 @@ if (__DEV__) {
249250
}
250251
}
251252

253+
if (__TEST__) {
254+
ReactDOM.act = act;
255+
}
256+
252257
export default ReactDOM;

0 commit comments

Comments
 (0)