Skip to content

Commit

Permalink
feat: Flush microtasks in cleanup (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds authored and kentcdodds committed Mar 12, 2020
1 parent fccc2cf commit 9fc8581
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/cleanup.js
@@ -1,7 +1,7 @@
import React from 'react'
import {render, cleanup} from '../'

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

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

render(<Test />)
cleanup()
await cleanup()
expect(document.body.innerHTML).toBe('')
expect(spy).toHaveBeenCalledTimes(1)
})

test('cleanup does not error when an element is not a child', () => {
test('cleanup does not error when an element is not a child', async () => {
render(<div />, {container: document.createElement('div')})
cleanup()
await cleanup()
})
4 changes: 1 addition & 3 deletions src/index.js
@@ -1,4 +1,3 @@
import flush from './flush-microtasks'
import {cleanup} from './pure'

// if we're running in a test runner that supports afterEach
Expand All @@ -8,8 +7,7 @@ import {cleanup} from './pure'
// or set the RTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await flush()
cleanup()
await cleanup()
})
}

Expand Down
4 changes: 3 additions & 1 deletion src/pure.js
Expand Up @@ -7,6 +7,7 @@ import {
configure as configureDTL,
} from '@testing-library/dom'
import act, {asyncAct} from './act-compat'
import flush from './flush-microtasks'

configureDTL({
asyncWrapper: async cb => {
Expand Down Expand Up @@ -88,7 +89,8 @@ function render(
}
}

function cleanup() {
async function cleanup() {
await flush()
mountedContainers.forEach(cleanupAtContainer)
}

Expand Down

0 comments on commit 9fc8581

Please sign in to comment.