diff --git a/src/__tests__/cleanup.js b/src/__tests__/cleanup.js index d1332bf4..f3f6a557 100644 --- a/src/__tests__/cleanup.js +++ b/src/__tests__/cleanup.js @@ -26,3 +26,17 @@ test('cleanup does not error when an element is not a child', async () => { render(
, {container: document.createElement('div')}) await cleanup() }) + +test('cleanup waits for queued microtasks during unmount sequence', async () => { + const spy = jest.fn() + + const Test = () => { + React.useEffect(() => () => setImmediate(spy)) + + return null + } + + render() + await cleanup() + expect(spy).toHaveBeenCalledTimes(1) +}) diff --git a/src/pure.js b/src/pure.js index fe7e6cf9..be0fa3f1 100644 --- a/src/pure.js +++ b/src/pure.js @@ -97,8 +97,10 @@ function render( } async function cleanup() { - await flush() mountedContainers.forEach(cleanupAtContainer) + // flush microtask queue after unmounting in case + // unmount sequence generates new microtasks + await flush() } // maybe one day we'll expose this (perhaps even as a utility returned by render).