From 565a073a7a93e8352b9b27a28d135873f2328df3 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Thu, 18 Nov 2021 12:24:25 +0100 Subject: [PATCH 1/2] feat: Enable "missing act" warnings in React 18 by default --- src/act-compat.js | 18 +++++++++++------- src/index.js | 14 ++++++++++++++ tests/setup-env.js | 8 -------- 3 files changed, 25 insertions(+), 15 deletions(-) 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..3118f114 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,19 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) { cleanup() }) } + + // This matches the behavior of React < 18. + if (typeof beforeAll === 'function' && typeof afterAll === 'function') { + 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 -}) From 3c5c261a4775f44f43c446dd7356973760828b98 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Thu, 18 Nov 2021 12:41:18 +0100 Subject: [PATCH 2/2] Coverage --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3118f114..bb0d0270 100644 --- a/src/index.js +++ b/src/index.js @@ -22,8 +22,10 @@ if (typeof process === 'undefined' || !process.env?.RTL_SKIP_AUTO_CLEANUP) { }) } - // This matches the behavior of React < 18. + // 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()