@@ -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 ;
0 commit comments