Skip to content

Commit 565a073

Browse files
committed
feat: Enable "missing act" warnings in React 18 by default
1 parent d8c6b4d commit 565a073

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/act-compat.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function getGlobalThis() {
3232
throw new Error('unable to locate global object')
3333
}
3434

35-
function setReactActEnvironment(isReactActEnvironment) {
35+
function setIsReactActEnvironment(isReactActEnvironment) {
3636
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment
3737
}
3838

@@ -43,7 +43,7 @@ function getIsReactActEnvironment() {
4343
function withGlobalActEnvironment(actImplementation) {
4444
return callback => {
4545
const previousActEnvironment = getIsReactActEnvironment()
46-
setReactActEnvironment(true)
46+
setIsReactActEnvironment(true)
4747
try {
4848
// The return value of `act` is always a thenable.
4949
let callbackNeedsToBeAwaited = false
@@ -64,24 +64,24 @@ function withGlobalActEnvironment(actImplementation) {
6464
then: (resolve, reject) => {
6565
thenable.then(
6666
returnValue => {
67-
setReactActEnvironment(previousActEnvironment)
67+
setIsReactActEnvironment(previousActEnvironment)
6868
resolve(returnValue)
6969
},
7070
error => {
71-
setReactActEnvironment(previousActEnvironment)
71+
setIsReactActEnvironment(previousActEnvironment)
7272
reject(error)
7373
},
7474
)
7575
},
7676
}
7777
} else {
78-
setReactActEnvironment(previousActEnvironment)
78+
setIsReactActEnvironment(previousActEnvironment)
7979
return actResult
8080
}
8181
} catch (error) {
8282
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
8383
// or if we have to await the callback first.
84-
setReactActEnvironment(previousActEnvironment)
84+
setIsReactActEnvironment(previousActEnvironment)
8585
throw error
8686
}
8787
}
@@ -203,6 +203,10 @@ function asyncAct(cb) {
203203
}
204204

205205
export default act
206-
export {asyncAct, setReactActEnvironment, getIsReactActEnvironment}
206+
export {
207+
asyncAct,
208+
setIsReactActEnvironment as setReactActEnvironment,
209+
getIsReactActEnvironment,
210+
}
207211

208212
/* eslint no-console:0 */

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {getIsReactActEnvironment, setReactActEnvironment} from './act-compat'
12
import {cleanup} from './pure'
23

34
// if we're running in a test runner that supports afterEach
@@ -20,6 +21,19 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) {
2021
cleanup()
2122
})
2223
}
24+
25+
// This matches the behavior of React < 18.
26+
if (typeof beforeAll === 'function' && typeof afterAll === 'function') {
27+
let previousIsReactActEnvironment = getIsReactActEnvironment()
28+
beforeAll(() => {
29+
previousIsReactActEnvironment = getIsReactActEnvironment()
30+
setReactActEnvironment(true)
31+
})
32+
33+
afterAll(() => {
34+
setReactActEnvironment(previousIsReactActEnvironment)
35+
})
36+
}
2337
}
2438

2539
export * from './pure'

tests/setup-env.js

-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
import '@testing-library/jest-dom/extend-expect'
2-
3-
beforeEach(() => {
4-
global.IS_REACT_ACT_ENVIRONMENT = true
5-
})
6-
7-
afterEach(() => {
8-
global.IS_REACT_ACT_ENVIRONMENT = false
9-
})

0 commit comments

Comments
 (0)