Skip to content

feat: Flush microtasks in cleanup #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/__tests__/cleanup.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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