Skip to content
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

feat: Enable "missing act" warnings in React 18 by default #994

Merged
merged 2 commits into from
Nov 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/act-compat.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
import '@testing-library/jest-dom/extend-expect'

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

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