-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Replace global jest
heuristic with IS_REACT_ACT_ENVIRONMENT
#22562
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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
130 changes: 130 additions & 0 deletions
130
packages/react-reconciler/src/__tests__/ReactActWarnings-test.js
This file contains 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,130 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @jest-environment node | ||
*/ | ||
|
||
let React; | ||
let Scheduler; | ||
let ReactNoop; | ||
let useState; | ||
let act; | ||
|
||
// These tests are mostly concerned with concurrent roots. The legacy root | ||
// behavior is covered by other older test suites and is unchanged from | ||
// React 17. | ||
describe('act warnings', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
Scheduler = require('scheduler'); | ||
ReactNoop = require('react-noop-renderer'); | ||
act = React.unstable_act; | ||
useState = React.useState; | ||
}); | ||
|
||
function Text(props) { | ||
Scheduler.unstable_yieldValue(props.text); | ||
return props.text; | ||
} | ||
|
||
function withActEnvironment(value, scope) { | ||
const prevValue = global.IS_REACT_ACT_ENVIRONMENT; | ||
global.IS_REACT_ACT_ENVIRONMENT = value; | ||
try { | ||
return scope(); | ||
} finally { | ||
global.IS_REACT_ACT_ENVIRONMENT = prevValue; | ||
} | ||
} | ||
|
||
test('warns about unwrapped updates only if environment flag is enabled', () => { | ||
let setState; | ||
function App() { | ||
const [state, _setState] = useState(0); | ||
setState = _setState; | ||
return <Text text={state} />; | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
root.render(<App />); | ||
expect(Scheduler).toFlushAndYield([0]); | ||
expect(root).toMatchRenderedOutput('0'); | ||
|
||
// Default behavior. Flag is undefined. No warning. | ||
expect(global.IS_REACT_ACT_ENVIRONMENT).toBe(undefined); | ||
setState(1); | ||
expect(Scheduler).toFlushAndYield([1]); | ||
expect(root).toMatchRenderedOutput('1'); | ||
|
||
// Flag is true. Warn. | ||
withActEnvironment(true, () => { | ||
expect(() => setState(2)).toErrorDev( | ||
'An update to App inside a test was not wrapped in act', | ||
); | ||
expect(Scheduler).toFlushAndYield([2]); | ||
expect(root).toMatchRenderedOutput('2'); | ||
}); | ||
|
||
// Flag is false. No warning. | ||
withActEnvironment(false, () => { | ||
setState(3); | ||
expect(Scheduler).toFlushAndYield([3]); | ||
expect(root).toMatchRenderedOutput('3'); | ||
}); | ||
}); | ||
|
||
// @gate __DEV__ | ||
test('act warns if the environment flag is not enabled', () => { | ||
let setState; | ||
function App() { | ||
const [state, _setState] = useState(0); | ||
setState = _setState; | ||
return <Text text={state} />; | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
root.render(<App />); | ||
expect(Scheduler).toFlushAndYield([0]); | ||
expect(root).toMatchRenderedOutput('0'); | ||
|
||
// Default behavior. Flag is undefined. Warn. | ||
expect(global.IS_REACT_ACT_ENVIRONMENT).toBe(undefined); | ||
expect(() => { | ||
act(() => { | ||
setState(1); | ||
}); | ||
}).toErrorDev( | ||
'The current testing environment is not configured to support act(...)', | ||
{withoutStack: true}, | ||
); | ||
expect(Scheduler).toHaveYielded([1]); | ||
expect(root).toMatchRenderedOutput('1'); | ||
|
||
// Flag is true. Don't warn. | ||
withActEnvironment(true, () => { | ||
act(() => { | ||
setState(2); | ||
}); | ||
expect(Scheduler).toHaveYielded([2]); | ||
expect(root).toMatchRenderedOutput('2'); | ||
}); | ||
|
||
// Flag is false. Warn. | ||
withActEnvironment(false, () => { | ||
expect(() => { | ||
act(() => { | ||
setState(1); | ||
}); | ||
}).toErrorDev( | ||
'The current testing environment is not configured to support act(...)', | ||
{withoutStack: true}, | ||
); | ||
expect(Scheduler).toHaveYielded([1]); | ||
expect(root).toMatchRenderedOutput('1'); | ||
}); | ||
}); | ||
}); |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These warnings no longer fire because our tests don't enable the act environment flag — we mock the Scheduler package instead, or (as in this test) manually flush the microtask queue.