GeezerVison is showing github.com — Expand act warning to cover all APIs that might schedule React work (… · react/react@d5b6b4b
File treeExpand file treeCollapse file tree Open diff view settings react-debug-tools/src/__tests__ react-devtools-scheduling-profiler/src/import-worker/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/__tests__/ReactDevToolsHooksIntegration-test.js +16-12Lines changed: 16 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,8 @@ describe('React hooks DevTools integration', () => { | 20 | 20 | let scheduleUpdate; | 21 | 21 | let setSuspenseHandler; | 22 | 22 |
| | 23 | + global.IS_REACT_ACT_ENVIRONMENT = true; | | 24 | + | 23 | 25 | beforeEach(() => { | 24 | 26 | global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = { | 25 | 27 | inject: injected => { | @@ -64,7 +66,7 @@ describe('React hooks DevTools integration', () => { | 64 | 66 | expect(stateHook.isStateEditable).toBe(true); | 65 | 67 |
| 66 | 68 | if (__DEV__) { | 67 | | - overrideHookState(fiber, stateHook.id, [], 10); | | 69 | + act(() => overrideHookState(fiber, stateHook.id, [], 10)); | 68 | 70 | expect(renderer.toJSON()).toEqual({ | 69 | 71 | type: 'div', | 70 | 72 | props: {}, | @@ -116,7 +118,7 @@ describe('React hooks DevTools integration', () => { | 116 | 118 | expect(reducerHook.isStateEditable).toBe(true); | 117 | 119 |
| 118 | 120 | if (__DEV__) { | 119 | | - overrideHookState(fiber, reducerHook.id, ['foo'], 'def'); | | 121 | + act(() => overrideHookState(fiber, reducerHook.id, ['foo'], 'def')); | 120 | 122 | expect(renderer.toJSON()).toEqual({ | 121 | 123 | type: 'div', | 122 | 124 | props: {}, | @@ -164,13 +166,12 @@ describe('React hooks DevTools integration', () => { | 164 | 166 | expect(stateHook.isStateEditable).toBe(true); | 165 | 167 |
| 166 | 168 | if (__DEV__) { | 167 | | - overrideHookState(fiber, stateHook.id, ['count'], 10); | | 169 | + act(() => overrideHookState(fiber, stateHook.id, ['count'], 10)); | 168 | 170 | expect(renderer.toJSON()).toEqual({ | 169 | 171 | type: 'div', | 170 | 172 | props: {}, | 171 | 173 | children: ['count:', '10'], | 172 | 174 | }); | 173 | | - | 174 | 175 | act(() => setStateFn(state => ({count: state.count + 1}))); | 175 | 176 | expect(renderer.toJSON()).toEqual({ | 176 | 177 | type: 'div', | @@ -233,7 +234,8 @@ describe('React hooks DevTools integration', () => { | 233 | 234 | } | 234 | 235 | }); | 235 | 236 |
| 236 | | - it('should support overriding suspense in concurrent mode', () => { | | 237 | + // @gate __DEV__ | | 238 | + it('should support overriding suspense in concurrent mode', async () => { | 237 | 239 | if (__DEV__) { | 238 | 240 | // Lock the first render | 239 | 241 | setSuspenseHandler(() => true); | @@ -243,13 +245,15 @@ describe('React hooks DevTools integration', () => { | 243 | 245 | return 'Done'; | 244 | 246 | } | 245 | 247 |
| 246 | | - const renderer = ReactTestRenderer.create( | 247 | | - <div> | 248 | | - <React.Suspense fallback={'Loading'}> | 249 | | - <MyComponent /> | 250 | | - </React.Suspense> | 251 | | - </div>, | 252 | | - {unstable_isConcurrent: true}, | | 248 | + const renderer = await act(() => | | 249 | + ReactTestRenderer.create( | | 250 | + <div> | | 251 | + <React.Suspense fallback={'Loading'}> | | 252 | + <MyComponent /> | | 253 | + </React.Suspense> | | 254 | + </div>, | | 255 | + {unstable_isConcurrent: true}, | | 256 | + ), | 253 | 257 | ); | 254 | 258 |
| 255 | 259 | expect(Scheduler).toFlushAndYield([]); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-scheduling-profiler/src/import-worker/__tests__/preprocessData-test.internal.js +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,8 +17,6 @@ import { | 17 | 17 | } from '../../constants'; | 18 | 18 | import REACT_VERSION from 'shared/ReactVersion'; | 19 | 19 |
| 20 | | -global.IS_REACT_ACT_ENVIRONMENT = true; | 21 | | - | 22 | 20 | describe('getLanesFromTransportDecimalBitmask', () => { | 23 | 21 | it('should return array of lane numbers from bitmask string', () => { | 24 | 22 | expect(getLanesFromTransportDecimalBitmask('1')).toEqual([0]); | @@ -210,6 +208,8 @@ describe('preprocessData', () => { | 210 | 208 | tid = 0; | 211 | 209 | pid = 0; | 212 | 210 | startTime = 0; | | 211 | + | | 212 | + global.IS_REACT_ACT_ENVIRONMENT = true; | 213 | 213 | }); | 214 | 214 |
| 215 | 215 | afterEach(() => { | @@ -1251,7 +1251,7 @@ describe('preprocessData', () => { | 1251 | 1251 |
| 1252 | 1252 | testMarks.push(...createUserTimingData(clearedMarks)); | 1253 | 1253 |
| 1254 | | - const data = await preprocessData(testMarks); | | 1254 | + const data = await act(() => preprocessData(testMarks)); | 1255 | 1255 | expect(data.suspenseEvents).toHaveLength(1); | 1256 | 1256 | expect(data.suspenseEvents[0].promiseName).toBe('Testing displayName'); | 1257 | 1257 | } | @@ -1367,6 +1367,8 @@ describe('preprocessData', () => { | 1367 | 1367 |
| 1368 | 1368 | const root = ReactDOM.createRoot(document.createElement('div')); | 1369 | 1369 |
| | 1370 | + // Temporarily turn off the act environment, since we're intentionally using Scheduler instead. | | 1371 | + global.IS_REACT_ACT_ENVIRONMENT = false; | 1370 | 1372 | React.startTransition(() => { | 1371 | 1373 | // Start rendering an async update (but don't finish). | 1372 | 1374 | root.render( | @@ -1837,7 +1839,7 @@ describe('preprocessData', () => { | 1837 | 1839 |
| 1838 | 1840 | testMarks.push(...createUserTimingData(clearedMarks)); | 1839 | 1841 |
| 1840 | | - const data = await preprocessData(testMarks); | | 1842 | + const data = await act(() => preprocessData(testMarks)); | 1841 | 1843 | expect(data.suspenseEvents).toHaveLength(1); | 1842 | 1844 | expect(data.suspenseEvents[0].warning).toMatchInlineSnapshot( | 1843 | 1845 | `"A component suspended during an update which caused a fallback to be shown. Consider using the Transition API to avoid hiding components after they've been mounted."`, | @@ -1895,7 +1897,7 @@ describe('preprocessData', () => { | 1895 | 1897 |
| 1896 | 1898 | testMarks.push(...createUserTimingData(clearedMarks)); | 1897 | 1899 |
| 1898 | | - const data = await preprocessData(testMarks); | | 1900 | + const data = await act(() => preprocessData(testMarks)); | 1899 | 1901 | expect(data.suspenseEvents).toHaveLength(1); | 1900 | 1902 | expect(data.suspenseEvents[0].warning).toBe(null); | 1901 | 1903 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js +29-9Lines changed: 29 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,18 +32,31 @@ describe('ReactTestUtils.act()', () => { | 32 | 32 | let concurrentRoot = null; | 33 | 33 | const renderConcurrent = (el, dom) => { | 34 | 34 | concurrentRoot = ReactDOM.createRoot(dom); | 35 | | - concurrentRoot.render(el); | | 35 | + if (__DEV__) { | | 36 | + act(() => concurrentRoot.render(el)); | | 37 | + } else { | | 38 | + concurrentRoot.render(el); | | 39 | + } | 36 | 40 | }; | 37 | 41 |
| 38 | 42 | const unmountConcurrent = _dom => { | 39 | | - if (concurrentRoot !== null) { | 40 | | - concurrentRoot.unmount(); | 41 | | - concurrentRoot = null; | | 43 | + if (__DEV__) { | | 44 | + act(() => { | | 45 | + if (concurrentRoot !== null) { | | 46 | + concurrentRoot.unmount(); | | 47 | + concurrentRoot = null; | | 48 | + } | | 49 | + }); | | 50 | + } else { | | 51 | + if (concurrentRoot !== null) { | | 52 | + concurrentRoot.unmount(); | | 53 | + concurrentRoot = null; | | 54 | + } | 42 | 55 | } | 43 | 56 | }; | 44 | 57 |
| 45 | 58 | const rerenderConcurrent = el => { | 46 | | - concurrentRoot.render(el); | | 59 | + act(() => concurrentRoot.render(el)); | 47 | 60 | }; | 48 | 61 |
| 49 | 62 | runActTests( | @@ -98,22 +111,29 @@ describe('ReactTestUtils.act()', () => { | 98 | 111 | ]); | 99 | 112 | }); | 100 | 113 |
| | 114 | + // @gate __DEV__ | 101 | 115 | it('does not warn in concurrent mode', () => { | 102 | 116 | const root = ReactDOM.createRoot(document.createElement('div')); | 103 | | - root.render(<App />); | | 117 | + act(() => root.render(<App />)); | 104 | 118 | Scheduler.unstable_flushAll(); | 105 | 119 | }); | 106 | 120 |
| 107 | 121 | it('warns in concurrent mode if root is strict', () => { | | 122 | + // TODO: We don't need this error anymore in concurrent mode because | | 123 | + // effects can only be scheduled as the result of an update, and we now | | 124 | + // enforce all updates must be wrapped with act, not just hook updates. | 108 | 125 | expect(() => { | 109 | 126 | const root = ReactDOM.createRoot(document.createElement('div'), { | 110 | 127 | unstable_strictMode: true, | 111 | 128 | }); | 112 | 129 | root.render(<App />); | 113 | | - Scheduler.unstable_flushAll(); | 114 | | - }).toErrorDev([ | | 130 | + }).toErrorDev( | | 131 | + 'An update to Root inside a test was not wrapped in act(...)', | | 132 | + {withoutStack: true}, | | 133 | + ); | | 134 | + expect(() => Scheduler.unstable_flushAll()).toErrorDev( | 115 | 135 | 'An update to App ran an effect, but was not wrapped in act(...)', | 116 | | - ]); | | 136 | + ); | 117 | 137 | }); | 118 | 138 | }); | 119 | 139 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberAct.new.js +29-25Lines changed: 29 additions & 25 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,43 +12,47 @@ import type {Fiber} from './ReactFiber.new'; | 12 | 12 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 13 | 13 |
| 14 | 14 | import {warnsIfNotActing} from './ReactFiberHostConfig'; | 15 | | -import {ConcurrentMode} from './ReactTypeOfMode'; | 16 | 15 |
| 17 | 16 | const {ReactCurrentActQueue} = ReactSharedInternals; | 18 | 17 |
| 19 | | -export function isActEnvironment(fiber: Fiber) { | | 18 | +export function isLegacyActEnvironment(fiber: Fiber) { | | 19 | + if (__DEV__) { | | 20 | + // Legacy mode. We preserve the behavior of React 17's act. It assumes an | | 21 | + // act environment whenever `jest` is defined, but you can still turn off | | 22 | + // spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly | | 23 | + // to false. | | 24 | + | | 25 | + const isReactActEnvironmentGlobal = | | 26 | + // $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global | | 27 | + typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' | | 28 | + ? IS_REACT_ACT_ENVIRONMENT | | 29 | + : undefined; | | 30 | + | | 31 | + // $FlowExpectedError - Flow doesn't know about jest | | 32 | + const jestIsDefined = typeof jest !== 'undefined'; | | 33 | + return ( | | 34 | + warnsIfNotActing && jestIsDefined && isReactActEnvironmentGlobal !== false | | 35 | + ); | | 36 | + } | | 37 | + return false; | | 38 | +} | | 39 | + | | 40 | +export function isConcurrentActEnvironment() { | 20 | 41 | if (__DEV__) { | 21 | 42 | const isReactActEnvironmentGlobal = | 22 | 43 | // $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global | 23 | 44 | typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' | 24 | 45 | ? IS_REACT_ACT_ENVIRONMENT | 25 | 46 | : undefined; | 26 | 47 |
| 27 | | - if (fiber.mode & ConcurrentMode) { | 28 | | - if ( | 29 | | - !isReactActEnvironmentGlobal && | 30 | | - ReactCurrentActQueue.current !== null | 31 | | - ) { | 32 | | - // TODO: Include link to relevant documentation page. | 33 | | - console.error( | 34 | | - 'The current testing environment is not configured to support ' + | 35 | | - 'act(...)', | 36 | | - ); | 37 | | - } | 38 | | - return isReactActEnvironmentGlobal; | 39 | | - } else { | 40 | | - // Legacy mode. We preserve the behavior of React 17's act. It assumes an | 41 | | - // act environment whenever `jest` is defined, but you can still turn off | 42 | | - // spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly | 43 | | - // to false. | 44 | | - // $FlowExpectedError - Flow doesn't know about jest | 45 | | - const jestIsDefined = typeof jest !== 'undefined'; | 46 | | - return ( | 47 | | - warnsIfNotActing && | 48 | | - jestIsDefined && | 49 | | - isReactActEnvironmentGlobal !== false | | 48 | + if (!isReactActEnvironmentGlobal && ReactCurrentActQueue.current !== null) { | | 49 | + // TODO: Include link to relevant documentation page. | | 50 | + console.error( | | 51 | + 'The current testing environment is not configured to support ' + | | 52 | + 'act(...)', | 50 | 53 | ); | 51 | 54 | } | | 55 | + return isReactActEnvironmentGlobal; | 52 | 56 | } | 53 | 57 | return false; | 54 | 58 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberAct.old.js +29-25Lines changed: 29 additions & 25 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,43 +12,47 @@ import type {Fiber} from './ReactFiber.old'; | 12 | 12 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 13 | 13 |
| 14 | 14 | import {warnsIfNotActing} from './ReactFiberHostConfig'; | 15 | | -import {ConcurrentMode} from './ReactTypeOfMode'; | 16 | 15 |
| 17 | 16 | const {ReactCurrentActQueue} = ReactSharedInternals; | 18 | 17 |
| 19 | | -export function isActEnvironment(fiber: Fiber) { | | 18 | +export function isLegacyActEnvironment(fiber: Fiber) { | | 19 | + if (__DEV__) { | | 20 | + // Legacy mode. We preserve the behavior of React 17's act. It assumes an | | 21 | + // act environment whenever `jest` is defined, but you can still turn off | | 22 | + // spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly | | 23 | + // to false. | | 24 | + | | 25 | + const isReactActEnvironmentGlobal = | | 26 | + // $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global | | 27 | + typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' | | 28 | + ? IS_REACT_ACT_ENVIRONMENT | | 29 | + : undefined; | | 30 | + | | 31 | + // $FlowExpectedError - Flow doesn't know about jest | | 32 | + const jestIsDefined = typeof jest !== 'undefined'; | | 33 | + return ( | | 34 | + warnsIfNotActing && jestIsDefined && isReactActEnvironmentGlobal !== false | | 35 | + ); | | 36 | + } | | 37 | + return false; | | 38 | +} | | 39 | + | | 40 | +export function isConcurrentActEnvironment() { | 20 | 41 | if (__DEV__) { | 21 | 42 | const isReactActEnvironmentGlobal = | 22 | 43 | // $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global | 23 | 44 | typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' | 24 | 45 | ? IS_REACT_ACT_ENVIRONMENT | 25 | 46 | : undefined; | 26 | 47 |
| 27 | | - if (fiber.mode & ConcurrentMode) { | 28 | | - if ( | 29 | | - !isReactActEnvironmentGlobal && | 30 | | - ReactCurrentActQueue.current !== null | 31 | | - ) { | 32 | | - // TODO: Include link to relevant documentation page. | 33 | | - console.error( | 34 | | - 'The current testing environment is not configured to support ' + | 35 | | - 'act(...)', | 36 | | - ); | 37 | | - } | 38 | | - return isReactActEnvironmentGlobal; | 39 | | - } else { | 40 | | - // Legacy mode. We preserve the behavior of React 17's act. It assumes an | 41 | | - // act environment whenever `jest` is defined, but you can still turn off | 42 | | - // spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly | 43 | | - // to false. | 44 | | - // $FlowExpectedError - Flow doesn't know about jest | 45 | | - const jestIsDefined = typeof jest !== 'undefined'; | 46 | | - return ( | 47 | | - warnsIfNotActing && | 48 | | - jestIsDefined && | 49 | | - isReactActEnvironmentGlobal !== false | | 48 | + if (!isReactActEnvironmentGlobal && ReactCurrentActQueue.current !== null) { | | 49 | + // TODO: Include link to relevant documentation page. | | 50 | + console.error( | | 51 | + 'The current testing environment is not configured to support ' + | | 52 | + 'act(...)', | 50 | 53 | ); | 51 | 54 | } | | 55 | + return isReactActEnvironmentGlobal; | 52 | 56 | } | 53 | 57 | return false; | 54 | 58 | } |
|