Skip to content

Commit 0e2cf7d

Browse files
committed
Use isomorphic act if available
1 parent 7911bdf commit 0e2cf7d

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

src/__tests__/no-act.js

+8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ afterEach(() => {
1212
consoleErrorMock.mockRestore()
1313
})
1414

15+
// no react-dom/test-utils also means no isomorphic act since isomorphic act got released after test-utils act
1516
jest.mock('react-dom/test-utils', () => ({}))
17+
jest.mock('react', () => {
18+
const ReactActual = jest.requireActual('react')
19+
20+
delete ReactActual.unstable_act
21+
22+
return ReactActual
23+
})
1624

1725
test('act works even when there is no act from test utils', () => {
1826
const callback = jest.fn()

src/act-compat.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as React from 'react'
22
import ReactDOM from 'react-dom'
33
import * as testUtils from 'react-dom/test-utils'
44

5-
const reactAct = testUtils.act
6-
const actSupported = reactAct !== undefined
5+
const isomorphicAct = React.unstable_act
6+
const domAct = testUtils.act
7+
const actSupported = domAct !== undefined
78

89
// act is supported [email protected]
910
// so for versions that don't have act from test utils
@@ -14,7 +15,7 @@ function actPolyfill(cb) {
1415
ReactDOM.render(<div />, document.createElement('div'))
1516
}
1617

17-
const act = reactAct || actPolyfill
18+
const act = isomorphicAct || domAct || actPolyfill
1819

1920
let youHaveBeenWarned = false
2021
let isAsyncActSupported = null
@@ -50,7 +51,7 @@ function asyncAct(cb) {
5051
}
5152
let cbReturn, result
5253
try {
53-
result = reactAct(() => {
54+
result = domAct(() => {
5455
cbReturn = cb()
5556
return cbReturn
5657
})

src/pure.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ configureDTL({
1414
eventWrapper: cb => {
1515
let result
1616
act(() => {
17-
// TODO: Remove ReactDOM.flushSync once `act` flushes the microtask queue.
18-
// Otherwise `act` wrapping updates that schedule microtask would need to be followed with `await null` to flush the microtask queue manually
19-
// See https://github.com/reactwg/react-18/discussions/21#discussioncomment-796755
20-
result = ReactDOM.flushSync(cb)
17+
result = cb()
2118
})
2219
return result
2320
},

tests/setup-env.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
import '@testing-library/jest-dom/extend-expect'
2-
3-
// TODO: Can be removed in a future React release: https://github.com/reactwg/react-18/discussions/23#discussioncomment-798952
4-
// eslint-disable-next-line import/no-extraneous-dependencies -- need the version from React not an explicitly declared one
5-
jest.mock('scheduler', () => require('scheduler/unstable_mock'))

0 commit comments

Comments
 (0)