diff --git a/src/act-compat.js b/src/act-compat.js index c8889e65..ea20e25e 100644 --- a/src/act-compat.js +++ b/src/act-compat.js @@ -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 } @@ -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 @@ -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 } } @@ -203,6 +203,10 @@ function asyncAct(cb) { } export default act -export {asyncAct, setReactActEnvironment, getIsReactActEnvironment} +export { + asyncAct, + setIsReactActEnvironment as setReactActEnvironment, + getIsReactActEnvironment, +} /* eslint no-console:0 */ diff --git a/src/index.js b/src/index.js index 96fbe155..bb0d0270 100644 --- a/src/index.js +++ b/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 @@ -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' diff --git a/tests/setup-env.js b/tests/setup-env.js index 6a5fcbee..264828a9 100644 --- a/tests/setup-env.js +++ b/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 -})