@@ -7,7 +7,7 @@ import ReactDOMClient from 'react-dom/client'
77// we call act only when rendering to flush any possible effects
88// usually the async nature of Vitest browser mode ensures consistency,
99// but rendering is sync and controlled by React directly
10- function act ( cb : ( ) => unknown ) {
10+ async function act ( cb : ( ) => unknown ) {
1111 // @ts -expect-error unstable_act is not typed, but exported
1212 const _act = React . act || React . unstable_act
1313 if ( typeof _act !== 'function' ) {
@@ -16,7 +16,7 @@ function act(cb: () => unknown) {
1616 else {
1717 ( globalThis as any ) . IS_REACT_ACT_ENVIRONMENT = true
1818 try {
19- _act ( cb )
19+ await _act ( cb )
2020 }
2121 finally {
2222 ; ( globalThis as any ) . IS_REACT_ACT_ENVIRONMENT = false
@@ -32,8 +32,8 @@ export interface RenderResult extends LocatorSelectors {
3232 maxLength ?: number ,
3333 options ?: PrettyDOMOptions
3434 ) => void
35- unmount : ( ) => void
36- rerender : ( ui : React . ReactNode ) => void
35+ unmount : ( ) => Promise < void >
36+ rerender : ( ui : React . ReactNode ) => Promise < void >
3737 asFragment : ( ) => DocumentFragment
3838}
3939
@@ -51,10 +51,10 @@ const mountedRootEntries: {
5151 root : ReturnType < typeof createConcurrentRoot >
5252} [ ] = [ ]
5353
54- export function render (
54+ export async function render (
5555 ui : React . ReactNode ,
5656 { container, baseElement, wrapper : WrapperComponent } : ComponentRenderOptions = { } ,
57- ) : RenderResult {
57+ ) : Promise < RenderResult > {
5858 if ( ! baseElement ) {
5959 // default to document.body instead of documentElement to avoid output of potentially-large
6060 // head elements (such as JSS style blocks) in debug output
@@ -87,7 +87,7 @@ export function render(
8787 } )
8888 }
8989
90- act ( ( ) => {
90+ await act ( ( ) => {
9191 root ! . render (
9292 strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
9393 )
@@ -97,13 +97,13 @@ export function render(
9797 container,
9898 baseElement,
9999 debug : ( el , maxLength , options ) => debug ( el , maxLength , options ) ,
100- unmount : ( ) => {
101- act ( ( ) => {
100+ unmount : async ( ) => {
101+ await act ( ( ) => {
102102 root . unmount ( )
103103 } )
104104 } ,
105- rerender : ( newUi : React . ReactNode ) => {
106- act ( ( ) => {
105+ rerender : async ( newUi : React . ReactNode ) => {
106+ await act ( ( ) => {
107107 root . render (
108108 strictModeIfNeeded ( wrapUiIfNeeded ( newUi , WrapperComponent ) ) ,
109109 )
@@ -128,7 +128,7 @@ export interface RenderHookResult<Result, Props> {
128128 /**
129129 * Triggers a re-render. The props will be passed to your renderHook callback.
130130 */
131- rerender : ( props ?: Props ) => void
131+ rerender : ( props ?: Props ) => Promise < void >
132132 /**
133133 * This is a stable reference to the latest value returned by your renderHook
134134 * callback
@@ -143,14 +143,14 @@ export interface RenderHookResult<Result, Props> {
143143 * Unmounts the test component. This is useful for when you need to test
144144 * any cleanup your useEffects have.
145145 */
146- unmount : ( ) => void
146+ unmount : ( ) => Promise < void >
147147 /**
148148 * A test helper to apply pending React updates before making assertions.
149149 */
150- act : ( callback : ( ) => unknown ) => void
150+ act : ( callback : ( ) => unknown ) => Promise < void >
151151}
152152
153- export function renderHook < Props , Result > ( renderCallback : ( initialProps ?: Props ) => Result , options : RenderHookOptions < Props > = { } ) : RenderHookResult < Result , Props > {
153+ export async function renderHook < Props , Result > ( renderCallback : ( initialProps ?: Props ) => Result , options : RenderHookOptions < Props > = { } ) : Promise < RenderHookResult < Result , Props > > {
154154 const { initialProps, ...renderOptions } = options
155155
156156 const result = React . createRef < Result > ( ) as unknown as { current : Result }
@@ -165,7 +165,7 @@ export function renderHook<Props, Result>(renderCallback: (initialProps?: Props)
165165 return null
166166 }
167167
168- const { rerender : baseRerender , unmount } = render (
168+ const { rerender : baseRerender , unmount } = await render (
169169 < TestComponent renderCallbackProps = { initialProps } /> ,
170170 renderOptions ,
171171 )
@@ -179,15 +179,15 @@ export function renderHook<Props, Result>(renderCallback: (initialProps?: Props)
179179 return { result, rerender, unmount, act }
180180}
181181
182- export function cleanup ( ) : void {
183- mountedRootEntries . forEach ( ( { root, container } ) => {
184- act ( ( ) => {
182+ export async function cleanup ( ) : Promise < void > {
183+ for ( const { root, container } of mountedRootEntries ) {
184+ await act ( ( ) => {
185185 root . unmount ( )
186186 } )
187187 if ( container . parentNode === document . body ) {
188188 document . body . removeChild ( container )
189189 }
190- } )
190+ }
191191 mountedRootEntries . length = 0
192192 mountedContainers . clear ( )
193193}
0 commit comments