Skip to content

Commit

Permalink
feat: Enable "missing act" warnings in React 18 by default (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Nov 23, 2021
1 parent d8c6b4d commit 8e536f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
18 changes: 11 additions & 7 deletions src/act-compat.js
Expand Up @@ -32,7 +32,7 @@ function getGlobalThis() {
throw new Error('unable to locate global object')
}

function setReactActEnvironment(isReactActEnvironment) {
function setIsReactActEnvironment(isReactActEnvironment) {
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment
}

Expand All @@ -43,7 +43,7 @@ function getIsReactActEnvironment() {
function withGlobalActEnvironment(actImplementation) {
return callback => {
const previousActEnvironment = getIsReactActEnvironment()
setReactActEnvironment(true)
setIsReactActEnvironment(true)
try {
// The return value of `act` is always a thenable.
let callbackNeedsToBeAwaited = false
Expand All @@ -64,24 +64,24 @@ function withGlobalActEnvironment(actImplementation) {
then: (resolve, reject) => {
thenable.then(
returnValue => {
setReactActEnvironment(previousActEnvironment)
setIsReactActEnvironment(previousActEnvironment)
resolve(returnValue)
},
error => {
setReactActEnvironment(previousActEnvironment)
setIsReactActEnvironment(previousActEnvironment)
reject(error)
},
)
},
}
} else {
setReactActEnvironment(previousActEnvironment)
setIsReactActEnvironment(previousActEnvironment)
return actResult
}
} catch (error) {
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
// or if we have to await the callback first.
setReactActEnvironment(previousActEnvironment)
setIsReactActEnvironment(previousActEnvironment)
throw error
}
}
Expand Down Expand Up @@ -203,6 +203,10 @@ function asyncAct(cb) {
}

export default act
export {asyncAct, setReactActEnvironment, getIsReactActEnvironment}
export {
asyncAct,
setIsReactActEnvironment as setReactActEnvironment,
getIsReactActEnvironment,
}

/* eslint no-console:0 */
16 changes: 16 additions & 0 deletions src/index.js
@@ -1,3 +1,4 @@
import {getIsReactActEnvironment, setReactActEnvironment} from './act-compat'
import {cleanup} from './pure'

// if we're running in a test runner that supports afterEach
Expand All @@ -20,6 +21,21 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) {
cleanup()
})
}

// No test setup with other test runners available
/* istanbul ignore else */
if (typeof beforeAll === 'function' && typeof afterAll === 'function') {
// This matches the behavior of React < 18.
let previousIsReactActEnvironment = getIsReactActEnvironment()
beforeAll(() => {
previousIsReactActEnvironment = getIsReactActEnvironment()
setReactActEnvironment(true)
})

afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment)
})
}
}

export * from './pure'
8 changes: 0 additions & 8 deletions tests/setup-env.js
@@ -1,9 +1 @@
import '@testing-library/jest-dom/extend-expect'

beforeEach(() => {
global.IS_REACT_ACT_ENVIRONMENT = true
})

afterEach(() => {
global.IS_REACT_ACT_ENVIRONMENT = false
})

0 comments on commit 8e536f8

Please sign in to comment.