Skip to content

Commit 1016edb

Browse files
Kent C. Doddskentcdodds
Kent C. Dodds
authored andcommitted
feat: Flush microtasks in cleanup (#519)
1 parent c36080c commit 1016edb

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

Diff for: src/__tests__/cleanup.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import {render, cleanup} from '../'
33

4-
test('cleans up the document', () => {
4+
test('cleans up the document', async () => {
55
const spy = jest.fn()
66
const divId = 'my-div'
77

@@ -17,12 +17,12 @@ test('cleans up the document', () => {
1717
}
1818

1919
render(<Test />)
20-
cleanup()
20+
await cleanup()
2121
expect(document.body.innerHTML).toBe('')
2222
expect(spy).toHaveBeenCalledTimes(1)
2323
})
2424

25-
test('cleanup does not error when an element is not a child', () => {
25+
test('cleanup does not error when an element is not a child', async () => {
2626
render(<div />, {container: document.createElement('div')})
27-
cleanup()
27+
await cleanup()
2828
})

Diff for: src/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import flush from './flush-microtasks'
21
import {cleanup} from './pure'
32

43
// if we're running in a test runner that supports afterEach
@@ -8,8 +7,7 @@ import {cleanup} from './pure'
87
// or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'.
98
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
109
afterEach(async () => {
11-
await flush()
12-
cleanup()
10+
await cleanup()
1311
})
1412
}
1513

Diff for: src/pure.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
configure as configureDTL,
88
} from '@testing-library/dom'
99
import act, {asyncAct} from './act-compat'
10+
import flush from './flush-microtasks'
1011

1112
configureDTL({
1213
asyncWrapper: async cb => {
@@ -88,7 +89,8 @@ function render(
8889
}
8990
}
9091

91-
function cleanup() {
92+
async function cleanup() {
93+
await flush();
9294
mountedContainers.forEach(cleanupAtContainer)
9395
}
9496

0 commit comments

Comments
 (0)