Skip to content

Commit 1107b96

Browse files
author
Sunil Pai
authoredFeb 6, 2019
[TestUtils.act] warn when using TestUtils.act in node (#14768)
* warn when using TestUtils.act in node * s/warns/throws * s/throw/warn * consistent ellipses
1 parent 0975ea3 commit 1107b96

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed
 

Diff for: ‎packages/react-dom/src/test-utils/ReactTestUtils.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ function validateClassInstance(inst, methodName) {
151151
);
152152
}
153153

154-
// stub element used by act() when flushing effects
155-
let actContainerElement = document.createElement('div');
154+
// a stub element, lazily initialized, used by act() when flushing effects
155+
let actContainerElement = null;
156156

157157
/**
158158
* Utilities for making it easy to test React components.
@@ -391,9 +391,25 @@ const ReactTestUtils = {
391391
SimulateNative: {},
392392

393393
act(callback: () => void): Thenable {
394+
if (actContainerElement === null) {
395+
// warn if we can't actually create the stub element
396+
if (__DEV__) {
397+
warningWithoutStack(
398+
typeof document !== 'undefined' &&
399+
document !== null &&
400+
typeof document.createElement === 'function',
401+
'It looks like you called TestUtils.act(...) in a non-browser environment. ' +
402+
"If you're using TestRenderer for your tests, you should call " +
403+
'TestRenderer.act(...) instead of TestUtils.act(...).',
404+
);
405+
}
406+
// then make it
407+
actContainerElement = document.createElement('div');
408+
}
409+
410+
const result = ReactDOM.unstable_batchedUpdates(callback);
394411
// note: keep these warning messages in sync with
395412
// createReactNoop.js and ReactTestRenderer.js
396-
const result = ReactDOM.unstable_batchedUpdates(callback);
397413
if (__DEV__) {
398414
if (result !== undefined) {
399415
let addendum;

Diff for: ‎packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ describe('ReactTestRenderer', () => {
10231023
});
10241024

10251025
describe('act', () => {
1026-
it('works', () => {
1026+
it('can use .act() to batch updates and effects', () => {
10271027
function App(props) {
10281028
React.useEffect(() => {
10291029
props.callback();
@@ -1043,5 +1043,19 @@ describe('ReactTestRenderer', () => {
10431043

10441044
expect(called).toBe(true);
10451045
});
1046+
it('warns and throws if you use TestUtils.act instead of TestRenderer.act in node', () => {
1047+
// we warn when you try to load 2 renderers in the same 'scope'
1048+
// so as suggested, we call resetModules() to carry on with the test
1049+
jest.resetModules();
1050+
const {act} = require('react-dom/test-utils');
1051+
expect(() => {
1052+
expect(() => act(() => {})).toThrow('document is not defined');
1053+
}).toWarnDev(
1054+
[
1055+
'It looks like you called TestUtils.act(...) in a non-browser environment',
1056+
],
1057+
{withoutStack: 1},
1058+
);
1059+
});
10461060
});
10471061
});

0 commit comments

Comments
 (0)
Please sign in to comment.