@@ -173,41 +173,49 @@ export function renderWithoutAct(
173173function createLegacyRoot ( container : ReactDOMClient . Container ) {
174174 return {
175175 render ( element : React . ReactNode ) {
176- withDisabledActEnvironment ( ( ) =>
177- ReactDOM . render ( element as unknown as React . ReactElement , container ) ,
178- )
176+ ReactDOM . render ( element as unknown as React . ReactElement , container )
179177 } ,
180178 unmount ( ) {
181- withDisabledActEnvironment ( ( ) =>
182- ReactDOM . unmountComponentAtNode ( container ) ,
183- )
179+ ReactDOM . unmountComponentAtNode ( container )
184180 } ,
185181 }
186182}
187183
188184function createConcurrentRoot ( container : ReactDOMClient . Container ) {
189- const root = withDisabledActEnvironment ( ( ) =>
190- ReactDOMClient . createRoot ( container ) ,
191- )
185+ const anyThis = globalThis as any as { IS_REACT_ACT_ENVIRONMENT ?: boolean }
186+ if ( anyThis . IS_REACT_ACT_ENVIRONMENT ) {
187+ throw new Error ( `Tried to create a React root for a render stream inside a React act environment.
188+ This is not supported. Please use \`disableActEnvironment\` to disable the act environment for this test.` )
189+ }
190+ const root = ReactDOMClient . createRoot ( container )
192191
193192 return {
194193 render ( element : React . ReactNode ) {
195- withDisabledActEnvironment ( ( ) => root . render ( element ) )
194+ if ( anyThis . IS_REACT_ACT_ENVIRONMENT ) {
195+ throw new Error ( `Tried to render a render stream inside a React act environment.
196+ This is not supported. Please use \`disableActEnvironment\` to disable the act environment for this test.` )
197+ }
198+ root . render ( element )
196199 } ,
197200 unmount ( ) {
198- withDisabledActEnvironment ( ( ) => root . unmount ( ) )
201+ root . unmount ( )
199202 } ,
200203 }
201204}
202205
203206export function cleanup ( ) {
204- mountedRootEntries . forEach ( ( { root, container} ) => {
205- root . unmount ( )
207+ // there is a good chance this happens outside of a test, where the user
208+ // has no control over enabling or disabling the React Act environment,
209+ // so we do it for them here.
210+ withDisabledActEnvironment ( ( ) => {
211+ mountedRootEntries . forEach ( ( { root, container} ) => {
212+ root . unmount ( )
206213
207- if ( container . parentNode === document . body ) {
208- document . body . removeChild ( container )
209- }
214+ if ( container . parentNode === document . body ) {
215+ document . body . removeChild ( container )
216+ }
217+ } )
218+ mountedRootEntries . length = 0
219+ mountedContainers . clear ( )
210220 } )
211- mountedRootEntries . length = 0
212- mountedContainers . clear ( )
213221}
0 commit comments