-
Notifications
You must be signed in to change notification settings - Fork 273
Switch to IS_REACT_ACT_ENVIRONMENT instead of act when needed when using react 18 #1131
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf63ab2
Switch to IS_REACT_ACT_ENVIRONMENT instead of act when needed when us…
AugustinLF 32b9288
Improve react 18 act support readability
AugustinLF 729626a
Improve readability
AugustinLF 8b6c16a
Update src/__tests__/waitFor.test.tsx
mdjastrzebski 1854997
Trigger new build
AugustinLF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,95 @@ | ||
import { act } from 'react-test-renderer'; | ||
// This file and the act() implementation is sourced from react-testing-library | ||
// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js | ||
import { act as reactTestRendererAct } from 'react-test-renderer'; | ||
AugustinLF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { checkReactVersionAtLeast } from './react-versions'; | ||
|
||
const actMock = (callback: () => void) => { | ||
callback(); | ||
}; | ||
|
||
export default act || actMock; | ||
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT | ||
declare global { | ||
var IS_REACT_ACT_ENVIRONMENT: boolean | undefined; | ||
} | ||
|
||
function setIsReactActEnvironment(isReactActEnvironment: boolean | undefined) { | ||
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment; | ||
} | ||
|
||
function getIsReactActEnvironment() { | ||
return globalThis.IS_REACT_ACT_ENVIRONMENT; | ||
} | ||
|
||
type Act = typeof reactTestRendererAct; | ||
AugustinLF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function withGlobalActEnvironment(actImplementation: Act) { | ||
return (callback: Parameters<Act>[0]) => { | ||
const previousActEnvironment = getIsReactActEnvironment(); | ||
setIsReactActEnvironment(true); | ||
|
||
// this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do | ||
try { | ||
// The return value of `act` is always a thenable. | ||
let callbackNeedsToBeAwaited = false; | ||
const actResult = actImplementation(() => { | ||
const result = callback(); | ||
if ( | ||
result !== null && | ||
typeof result === 'object' && | ||
// @ts-expect-error this should be a promise or thenable | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
typeof result.then === 'function' | ||
) { | ||
callbackNeedsToBeAwaited = true; | ||
} | ||
return result; | ||
}); | ||
if (callbackNeedsToBeAwaited) { | ||
const thenable = actResult; | ||
return { | ||
then: ( | ||
resolve: (value: never) => never, | ||
reject: (value: never) => never | ||
AugustinLF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) => { | ||
// eslint-disable-next-line | ||
thenable.then( | ||
// eslint-disable-next-line promise/always-return | ||
(returnValue) => { | ||
setIsReactActEnvironment(previousActEnvironment); | ||
resolve(returnValue); | ||
}, | ||
(error) => { | ||
setIsReactActEnvironment(previousActEnvironment); | ||
reject(error); | ||
} | ||
); | ||
}, | ||
}; | ||
} else { | ||
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. | ||
setIsReactActEnvironment(previousActEnvironment); | ||
throw error; | ||
} | ||
}; | ||
} | ||
const getAct = () => { | ||
if (!reactTestRendererAct) { | ||
return actMock; | ||
} | ||
|
||
return checkReactVersionAtLeast(18, 0) | ||
? withGlobalActEnvironment(reactTestRendererAct) | ||
: reactTestRendererAct; | ||
}; | ||
const act = getAct(); | ||
|
||
export default act; | ||
export { | ||
setIsReactActEnvironment as setReactActEnvironment, | ||
getIsReactActEnvironment, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import { cleanup } from './pure'; | ||
import { flushMicroTasks } from './flushMicroTasks'; | ||
import { getIsReactActEnvironment, setReactActEnvironment } from './act'; | ||
|
||
// If we're running in a test runner that supports afterEach | ||
// then we'll automatically run cleanup afterEach test | ||
// this ensures that tests run in isolation from each other | ||
// if you don't like this then either import the `pure` module | ||
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'. | ||
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) { | ||
// eslint-disable-next-line no-undef | ||
afterEach(async () => { | ||
await flushMicroTasks(); | ||
cleanup(); | ||
}); | ||
if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) { | ||
// If we're running in a test runner that supports afterEach | ||
// then we'll automatically run cleanup afterEach test | ||
// this ensures that tests run in isolation from each other | ||
// if you don't like this then either import the `pure` module | ||
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'. | ||
if (typeof afterEach === 'function') { | ||
// eslint-disable-next-line no-undef | ||
afterEach(async () => { | ||
await flushMicroTasks(); | ||
cleanup(); | ||
}); | ||
} | ||
|
||
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as React from 'react'; | ||
|
||
export function checkReactVersionAtLeast( | ||
major: number, | ||
minor: number | ||
): boolean { | ||
if (React.version === undefined) return false; | ||
const [actualMajor, actualMinor] = React.version.split('.').map(Number); | ||
|
||
return actualMajor > major || (actualMajor === major && actualMinor >= minor); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.