Skip to content

await act(...) inside cleanup-after-each #427

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
Aug 6, 2019
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
4 changes: 3 additions & 1 deletion cleanup-after-each.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
afterEach(require('./dist').cleanup)
afterEach(() => {
return require('./dist/cleanup-async')()
})
32 changes: 32 additions & 0 deletions src/__tests__/cleanup-after-each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react'
import {render} from '../index'
import cleanupAsync from '../cleanup-async'

afterEach(() => {
return cleanupAsync()
})

const log = []
let ctr = 0

function App() {
async function somethingAsync() {
await null
log.push(ctr++)
}
React.useEffect(() => {
somethingAsync()
}, [])
return 123
}

it('cleanup-after-each does not leave any hanging microtasks: part 1', () => {
render(<App />)
expect(document.body.textContent).toBe('123')
expect(log).toEqual([])
})

it('cleanup-after-each does not leave any hanging microtasks: part 2', () => {
expect(log).toEqual([0])
expect(document.body.innerHTML).toBe('')
})
2 changes: 1 addition & 1 deletion src/act-compat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import ReactDOM from 'react-dom'
import {reactDomSixteenPointNineIsReleased} from './react-dom-16.9.0-is-released'
import * as testUtils from 'react-dom/test-utils'
import {reactDomSixteenPointNineIsReleased} from './react-dom-16.9.0-is-released'

const reactAct = testUtils.act
const actSupported = reactAct !== undefined
Expand Down
10 changes: 10 additions & 0 deletions src/cleanup-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is for use by the top-level export
// @testing-library/react/cleanup-after-each
// It is not meant to be used directly

module.exports = async function cleanupAsync() {
const {asyncAct} = require('./act-compat')
const {cleanup} = require('./index')
await asyncAct(async () => {})
cleanup()
}