Skip to content

Commit 091cec7

Browse files
author
Sebastian Silbermann
committed
Fix overlapping act call during when using cleanup
1 parent 7315eb8 commit 091cec7

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/__tests__/cleanup.js

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ test('cleanup runs effect cleanup functions', async () => {
4141
expect(spy).toHaveBeenCalledTimes(1)
4242
})
4343

44+
test('cleanup cleans up every root and disconnects containers', async () => {
45+
const {container: container1} = await render(<div />)
46+
const {container: container2} = await render(<span />)
47+
48+
await cleanup()
49+
50+
expect(container1).toBeEmptyDOMElement()
51+
expect(container1.isConnected).toBe(false)
52+
expect(container2).toBeEmptyDOMElement()
53+
expect(container2.isConnected).toBe(false)
54+
})
55+
4456
describe('fake timers and missing act warnings', () => {
4557
beforeEach(() => {
4658
jest.resetAllMocks()

src/pure.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,14 @@ async function render(
214214
}
215215

216216
async function cleanup() {
217-
await Promise.all(
218-
mountedRootEntries.map(async ({root, container}) => {
219-
await act(() => {
220-
root.unmount()
221-
})
222-
if (container.parentNode === document.body) {
223-
document.body.removeChild(container)
224-
}
225-
}),
226-
)
217+
for (const {container, root} of mountedRootEntries) {
218+
// eslint-disable-next-line no-await-in-loop -- Overlapping act calls are not allowed.
219+
await root.unmount()
220+
if (container.parentNode === document.body) {
221+
document.body.removeChild(container)
222+
}
223+
}
224+
227225
mountedRootEntries.length = 0
228226
mountedContainers.clear()
229227
}

0 commit comments

Comments
 (0)