@@ -173,41 +173,49 @@ export function renderWithoutAct(
173
173
function createLegacyRoot ( container : ReactDOMClient . Container ) {
174
174
return {
175
175
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 )
179
177
} ,
180
178
unmount ( ) {
181
- withDisabledActEnvironment ( ( ) =>
182
- ReactDOM . unmountComponentAtNode ( container ) ,
183
- )
179
+ ReactDOM . unmountComponentAtNode ( container )
184
180
} ,
185
181
}
186
182
}
187
183
188
184
function 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 )
192
191
193
192
return {
194
193
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 )
196
199
} ,
197
200
unmount ( ) {
198
- withDisabledActEnvironment ( ( ) => root . unmount ( ) )
201
+ root . unmount ( )
199
202
} ,
200
203
}
201
204
}
202
205
203
206
export 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 ( )
206
213
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 ( )
210
220
} )
211
- mountedRootEntries . length = 0
212
- mountedContainers . clear ( )
213
221
}
0 commit comments