Skip to content

Commit 8d70265

Browse files
author
Kent C. Dodds
committed
fix(act): add a polyfill
Thanks @threepointone!
1 parent a4b5307 commit 8d70265

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/__tests__/no-act.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ test('act works even when there is no act from test utils', () => {
66
const callback = jest.fn()
77
act(callback)
88
expect(callback).toHaveBeenCalledTimes(1)
9-
expect(callback).toHaveBeenCalledWith(/* nothing */)
109
})

src/act-compat.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import ReactDOM from 'react-dom'
12
import {act as reactAct} from 'react-dom/test-utils'
23

34
// act is supported react-dom@16.8.0
4-
// and is only needed for versions higher than that
5-
// so we do nothing for versions that don't support act.
6-
const act = reactAct || (cb => cb())
5+
// so for versions that don't have act from test utils
6+
// we do this little polyfill. No warnings, but it's
7+
// better than nothing.
8+
function actPolyfill(cb) {
9+
ReactDOM.unstable_batchedUpdates(cb)
10+
ReactDOM.render(null, document.createElement('div'))
11+
}
12+
13+
const act = reactAct || actPolyfill
714

815
function rtlAct(...args) {
916
return act(...args)

0 commit comments

Comments
 (0)