11import React , { Suspense } from 'react'
22import { act , create } from 'react-test-renderer'
33
4+ let cleanupCallbacks = [ ]
5+
46function TestHook ( { callback, hookProps, onError, children } ) {
57 try {
68 children ( callback ( hookProps ) )
@@ -73,6 +75,15 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
7375 } )
7476 const { unmount, update } = testRenderer
7577
78+ function unmountHook ( ) {
79+ act ( ( ) => {
80+ cleanupCallbacks = cleanupCallbacks . filter ( ( cb ) => cb !== unmountHook )
81+ unmount ( )
82+ } )
83+ }
84+
85+ cleanupCallbacks . push ( unmountHook )
86+
7687 let waitingForNextUpdate = null
7788 const resolveOnNextUpdate = ( resolve ) => {
7889 addResolver ( ( ...args ) => {
@@ -93,12 +104,22 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
93104 update ( toRender ( ) )
94105 } )
95106 } ,
96- unmount : ( ) => {
97- act ( ( ) => {
98- unmount ( )
99- } )
100- }
107+ unmount : unmountHook
101108 }
102109}
103110
104- export { renderHook , act }
111+ async function cleanup ( ) {
112+ await act ( async ( ) => {
113+ await act ( async ( ) => { } )
114+ cleanupCallbacks . forEach ( ( cb ) => cb ( ) )
115+ cleanupCallbacks = [ ]
116+ } )
117+ }
118+
119+ if ( typeof afterEach === 'function' && ! process . env . RHTL_SKIP_AUTO_CLEANUP ) {
120+ afterEach ( async ( ) => {
121+ await cleanup ( )
122+ } )
123+ }
124+
125+ export { renderHook , cleanup , act }
0 commit comments