Skip to content

Commit aac2e44

Browse files
authored
fix(cleanup): Cleanup should flush microtask queue after unmount (#632)
1 parent 5814b29 commit aac2e44

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/__tests__/cleanup.js

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ test('cleanup does not error when an element is not a child', async () => {
2626
render(<div />, {container: document.createElement('div')})
2727
await cleanup()
2828
})
29+
30+
test('cleanup waits for queued microtasks during unmount sequence', async () => {
31+
const spy = jest.fn()
32+
33+
const Test = () => {
34+
React.useEffect(() => () => setImmediate(spy))
35+
36+
return null
37+
}
38+
39+
render(<Test />)
40+
await cleanup()
41+
expect(spy).toHaveBeenCalledTimes(1)
42+
})

src/pure.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ function render(
9797
}
9898

9999
async function cleanup() {
100-
await flush()
101100
mountedContainers.forEach(cleanupAtContainer)
101+
// flush microtask queue after unmounting in case
102+
// unmount sequence generates new microtasks
103+
await flush()
102104
}
103105

104106
// maybe one day we'll expose this (perhaps even as a utility returned by render).

0 commit comments

Comments
 (0)