GeezerVison is showing github.com — Throw when `act` is used in production (#21686) · react/react@bd0a963
File treeExpand file treeCollapse file tree Open diff view settings fixtures/legacy-jsx-runtimes react-test-renderer/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/legacy-jsx-runtimes/setupTests.js -10Lines changed: 0 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,16 +33,6 @@ function shouldIgnoreConsoleError(format, args) { | 33 | 33 | // They are noisy too so we'll try to ignore them. | 34 | 34 | return true; | 35 | 35 | } | 36 | | - if ( | 37 | | - format.indexOf( | 38 | | - 'act(...) is not supported in production builds of React' | 39 | | - ) === 0 | 40 | | - ) { | 41 | | - // We don't yet support act() for prod builds, and warn for it. | 42 | | - // But we'd like to use act() ourselves for prod builds. | 43 | | - // Let's ignore the warning and #yolo. | 44 | | - return true; | 45 | | - } | 46 | 36 | } | 47 | 37 | // Looks legit | 48 | 38 | return false; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js +28-18Lines changed: 28 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -135,6 +135,7 @@ function runActTests(label, render, unmount, rerender) { | 135 | 135 | }); | 136 | 136 |
| 137 | 137 | describe('sync', () => { | | 138 | + // @gate __DEV__ | 138 | 139 | it('can use act to flush effects', () => { | 139 | 140 | function App() { | 140 | 141 | React.useEffect(() => { | @@ -150,6 +151,7 @@ function runActTests(label, render, unmount, rerender) { | 150 | 151 | expect(Scheduler).toHaveYielded([100]); | 151 | 152 | }); | 152 | 153 |
| | 154 | + // @gate __DEV__ | 153 | 155 | it('flushes effects on every call', async () => { | 154 | 156 | function App() { | 155 | 157 | const [ctr, setCtr] = React.useState(0); | @@ -186,6 +188,7 @@ function runActTests(label, render, unmount, rerender) { | 186 | 188 | expect(button.innerHTML).toBe('5'); | 187 | 189 | }); | 188 | 190 |
| | 191 | + // @gate __DEV__ | 189 | 192 | it("should keep flushing effects until they're done", () => { | 190 | 193 | function App() { | 191 | 194 | const [ctr, setCtr] = React.useState(0); | @@ -204,6 +207,7 @@ function runActTests(label, render, unmount, rerender) { | 204 | 207 | expect(container.innerHTML).toBe('5'); | 205 | 208 | }); | 206 | 209 |
| | 210 | + // @gate __DEV__ | 207 | 211 | it('should flush effects only on exiting the outermost act', () => { | 208 | 212 | function App() { | 209 | 213 | React.useEffect(() => { | @@ -224,6 +228,7 @@ function runActTests(label, render, unmount, rerender) { | 224 | 228 | expect(Scheduler).toHaveYielded([0]); | 225 | 229 | }); | 226 | 230 |
| | 231 | + // @gate __DEV__ | 227 | 232 | it('warns if a setState is called outside of act(...)', () => { | 228 | 233 | let setValue = null; | 229 | 234 | function App() { | @@ -250,6 +255,7 @@ function runActTests(label, render, unmount, rerender) { | 250 | 255 | jest.useRealTimers(); | 251 | 256 | }); | 252 | 257 |
| | 258 | + // @gate __DEV__ | 253 | 259 | it('lets a ticker update', () => { | 254 | 260 | function App() { | 255 | 261 | const [toggle, setToggle] = React.useState(0); | @@ -272,6 +278,7 @@ function runActTests(label, render, unmount, rerender) { | 272 | 278 | expect(container.innerHTML).toBe('1'); | 273 | 279 | }); | 274 | 280 |
| | 281 | + // @gate __DEV__ | 275 | 282 | it('can use the async version to catch microtasks', async () => { | 276 | 283 | function App() { | 277 | 284 | const [toggle, setToggle] = React.useState(0); | @@ -294,6 +301,7 @@ function runActTests(label, render, unmount, rerender) { | 294 | 301 | expect(container.innerHTML).toBe('1'); | 295 | 302 | }); | 296 | 303 |
| | 304 | + // @gate __DEV__ | 297 | 305 | it('can handle cascading promises with fake timers', async () => { | 298 | 306 | // this component triggers an effect, that waits a tick, | 299 | 307 | // then sets state. repeats this 5 times. | @@ -317,6 +325,7 @@ function runActTests(label, render, unmount, rerender) { | 317 | 325 | expect(container.innerHTML).toBe('5'); | 318 | 326 | }); | 319 | 327 |
| | 328 | + // @gate __DEV__ | 320 | 329 | it('flushes immediate re-renders with act', () => { | 321 | 330 | function App() { | 322 | 331 | const [ctr, setCtr] = React.useState(0); | @@ -346,6 +355,7 @@ function runActTests(label, render, unmount, rerender) { | 346 | 355 | }); | 347 | 356 | }); | 348 | 357 |
| | 358 | + // @gate __DEV__ | 349 | 359 | it('warns if you return a value inside act', () => { | 350 | 360 | expect(() => act(() => null)).toErrorDev( | 351 | 361 | [ | @@ -361,6 +371,7 @@ function runActTests(label, render, unmount, rerender) { | 361 | 371 | ); | 362 | 372 | }); | 363 | 373 |
| | 374 | + // @gate __DEV__ | 364 | 375 | it('warns if you try to await a sync .act call', () => { | 365 | 376 | expect(() => act(() => {}).then(() => {})).toErrorDev( | 366 | 377 | [ | @@ -372,6 +383,7 @@ function runActTests(label, render, unmount, rerender) { | 372 | 383 | }); | 373 | 384 |
| 374 | 385 | describe('asynchronous tests', () => { | | 386 | + // @gate __DEV__ | 375 | 387 | it('works with timeouts', async () => { | 376 | 388 | function App() { | 377 | 389 | const [ctr, setCtr] = React.useState(0); | @@ -396,6 +408,7 @@ function runActTests(label, render, unmount, rerender) { | 396 | 408 | expect(container.innerHTML).toBe('1'); | 397 | 409 | }); | 398 | 410 |
| | 411 | + // @gate __DEV__ | 399 | 412 | it('flushes microtasks before exiting', async () => { | 400 | 413 | function App() { | 401 | 414 | const [ctr, setCtr] = React.useState(0); | @@ -418,6 +431,7 @@ function runActTests(label, render, unmount, rerender) { | 418 | 431 | expect(container.innerHTML).toEqual('1'); | 419 | 432 | }); | 420 | 433 |
| | 434 | + // @gate __DEV__ | 421 | 435 | it('warns if you do not await an act call', async () => { | 422 | 436 | spyOnDevAndProd(console, 'error'); | 423 | 437 | act(async () => {}); | @@ -431,6 +445,7 @@ function runActTests(label, render, unmount, rerender) { | 431 | 445 | } | 432 | 446 | }); | 433 | 447 |
| | 448 | + // @gate __DEV__ | 434 | 449 | it('warns if you try to interleave multiple act calls', async () => { | 435 | 450 | spyOnDevAndProd(console, 'error'); | 436 | 451 | // let's try to cheat and spin off a 'thread' with an act call | @@ -450,6 +465,7 @@ function runActTests(label, render, unmount, rerender) { | 450 | 465 | } | 451 | 466 | }); | 452 | 467 |
| | 468 | + // @gate __DEV__ | 453 | 469 | it('async commits and effects are guaranteed to be flushed', async () => { | 454 | 470 | function App() { | 455 | 471 | const [state, setState] = React.useState(0); | @@ -475,6 +491,7 @@ function runActTests(label, render, unmount, rerender) { | 475 | 491 | expect(container.innerHTML).toBe('1'); | 476 | 492 | }); | 477 | 493 |
| | 494 | + // @gate __DEV__ | 478 | 495 | it('can handle cascading promises', async () => { | 479 | 496 | // this component triggers an effect, that waits a tick, | 480 | 497 | // then sets state. repeats this 5 times. | @@ -501,6 +518,7 @@ function runActTests(label, render, unmount, rerender) { | 501 | 518 | }); | 502 | 519 |
| 503 | 520 | describe('error propagation', () => { | | 521 | + // @gate __DEV__ | 504 | 522 | it('propagates errors - sync', () => { | 505 | 523 | let err; | 506 | 524 | try { | @@ -515,6 +533,7 @@ function runActTests(label, render, unmount, rerender) { | 515 | 533 | } | 516 | 534 | }); | 517 | 535 |
| | 536 | + // @gate __DEV__ | 518 | 537 | it('should propagate errors from effects - sync', () => { | 519 | 538 | function App() { | 520 | 539 | React.useEffect(() => { | @@ -536,6 +555,7 @@ function runActTests(label, render, unmount, rerender) { | 536 | 555 | } | 537 | 556 | }); | 538 | 557 |
| | 558 | + // @gate __DEV__ | 539 | 559 | it('propagates errors - async', async () => { | 540 | 560 | let err; | 541 | 561 | try { | @@ -551,6 +571,7 @@ function runActTests(label, render, unmount, rerender) { | 551 | 571 | } | 552 | 572 | }); | 553 | 573 |
| | 574 | + // @gate __DEV__ | 554 | 575 | it('should cleanup after errors - sync', () => { | 555 | 576 | function App() { | 556 | 577 | React.useEffect(() => { | @@ -576,6 +597,7 @@ function runActTests(label, render, unmount, rerender) { | 576 | 597 | } | 577 | 598 | }); | 578 | 599 |
| | 600 | + // @gate __DEV__ | 579 | 601 | it('should cleanup after errors - async', async () => { | 580 | 602 | function App() { | 581 | 603 | async function somethingAsync() { | @@ -611,6 +633,7 @@ function runActTests(label, render, unmount, rerender) { | 611 | 633 | if (__DEV__ && __EXPERIMENTAL__) { | 612 | 634 | // todo - remove __DEV__ check once we start using testing builds | 613 | 635 |
| | 636 | + // @gate __DEV__ | 614 | 637 | it('triggers fallbacks if available', async () => { | 615 | 638 | if (label !== 'legacy mode') { | 616 | 639 | // FIXME: Support for Concurrent Root intentionally removed | @@ -691,25 +714,12 @@ function runActTests(label, render, unmount, rerender) { | 691 | 714 | }); | 692 | 715 | } | 693 | 716 | }); | 694 | | - describe('warn in prod mode', () => { | | 717 | + describe('throw in prod mode', () => { | | 718 | + // @gate !__DEV__ | 695 | 719 | it('warns if you try to use act() in prod mode', () => { | 696 | | - const spy = spyOnDevAndProd(console, 'error'); | 697 | | - | 698 | | - act(() => {}); | 699 | | - | 700 | | - if (!__DEV__) { | 701 | | - expect(console.error).toHaveBeenCalledTimes(1); | 702 | | - expect(console.error.calls.argsFor(0)[0]).toContain( | 703 | | - 'act(...) is not supported in production builds of React', | 704 | | - ); | 705 | | - } else { | 706 | | - expect(console.error).toHaveBeenCalledTimes(0); | 707 | | - } | 708 | | - | 709 | | - spy.calls.reset(); | 710 | | - // does not warn twice | 711 | | - act(() => {}); | 712 | | - expect(console.error).toHaveBeenCalledTimes(0); | | 720 | + expect(() => act(() => {})).toThrow( | | 721 | + 'act(...) is not supported in production builds of React', | | 722 | + ); | 713 | 723 | }); | 714 | 724 | }); | 715 | 725 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactTestUtilsActUnmockedScheduler-test.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,6 +47,7 @@ afterEach(() => { | 47 | 47 | document.body.removeChild(container); | 48 | 48 | }); | 49 | 49 |
| | 50 | +// @gate __DEV__ | 50 | 51 | it('can use act to flush effects', () => { | 51 | 52 | function App() { | 52 | 53 | React.useEffect(() => { | @@ -62,6 +63,7 @@ it('can use act to flush effects', () => { | 62 | 63 | expect(clearYields()).toEqual([100]); | 63 | 64 | }); | 64 | 65 |
| | 66 | +// @gate __DEV__ | 65 | 67 | it('flushes effects on every call', () => { | 66 | 68 | function App() { | 67 | 69 | const [ctr, setCtr] = React.useState(0); | @@ -100,6 +102,7 @@ it('flushes effects on every call', () => { | 100 | 102 | expect(button.innerHTML).toEqual('5'); | 101 | 103 | }); | 102 | 104 |
| | 105 | +// @gate __DEV__ | 103 | 106 | it("should keep flushing effects until they're done", () => { | 104 | 107 | function App() { | 105 | 108 | const [ctr, setCtr] = React.useState(0); | @@ -118,6 +121,7 @@ it("should keep flushing effects until they're done", () => { | 118 | 121 | expect(container.innerHTML).toEqual('5'); | 119 | 122 | }); | 120 | 123 |
| | 124 | +// @gate __DEV__ | 121 | 125 | it('should flush effects only on exiting the outermost act', () => { | 122 | 126 | function App() { | 123 | 127 | React.useEffect(() => { | @@ -138,6 +142,7 @@ it('should flush effects only on exiting the outermost act', () => { | 138 | 142 | expect(clearYields()).toEqual([0]); | 139 | 143 | }); | 140 | 144 |
| | 145 | +// @gate __DEV__ | 141 | 146 | it('can handle cascading promises', async () => { | 142 | 147 | // this component triggers an effect, that waits a tick, | 143 | 148 | // then sets state. repeats this 5 times. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/test-utils/ReactTestUtilsPublicAct.js +5-8Lines changed: 5 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import * as ReactDOM from 'react-dom'; | 13 | 13 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 14 | 14 | import enqueueTask from 'shared/enqueueTask'; | 15 | 15 | import * as Scheduler from 'scheduler'; | | 16 | +import invariant from 'shared/invariant'; | 16 | 17 |
| 17 | 18 | // Keep in sync with ReactDOM.js, and ReactTestUtils.js: | 18 | 19 | const EventInternals = | @@ -71,17 +72,13 @@ function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) { | 71 | 72 | // so we can tell if any async act() calls try to run in parallel. | 72 | 73 |
| 73 | 74 | let actingUpdatesScopeDepth = 0; | 74 | | -let didWarnAboutUsingActInProd = false; | 75 | 75 |
| 76 | 76 | export function act(callback: () => Thenable<mixed>): Thenable<void> { | 77 | 77 | if (!__DEV__) { | 78 | | - if (didWarnAboutUsingActInProd === false) { | 79 | | - didWarnAboutUsingActInProd = true; | 80 | | - // eslint-disable-next-line react-internal/no-production-logging | 81 | | - console.error( | 82 | | - 'act(...) is not supported in production builds of React, and might not behave as expected.', | 83 | | - ); | 84 | | - } | | 78 | + invariant( | | 79 | + false, | | 80 | + 'act(...) is not supported in production builds of React.', | | 81 | + ); | 85 | 82 | } | 86 | 83 | const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth; | 87 | 84 | actingUpdatesScopeDepth++; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2985,17 +2985,13 @@ function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) { | 2985 | 2985 | // so we can tell if any async act() calls try to run in parallel. | 2986 | 2986 |
| 2987 | 2987 | let actingUpdatesScopeDepth = 0; | 2988 | | -let didWarnAboutUsingActInProd = false; | 2989 | 2988 |
| 2990 | 2989 | export function act(callback: () => Thenable<mixed>): Thenable<void> { | 2991 | 2990 | if (!__DEV__) { | 2992 | | - if (didWarnAboutUsingActInProd === false) { | 2993 | | - didWarnAboutUsingActInProd = true; | 2994 | | - // eslint-disable-next-line react-internal/no-production-logging | 2995 | | - console.error( | 2996 | | - 'act(...) is not supported in production builds of React, and might not behave as expected.', | 2997 | | - ); | 2998 | | - } | | 2991 | + invariant( | | 2992 | + false, | | 2993 | + 'act(...) is not supported in production builds of React.', | | 2994 | + ); | 2999 | 2995 | } | 3000 | 2996 |
| 3001 | 2997 | const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2985,17 +2985,13 @@ function flushWorkAndMicroTasks(onDone: (err: ?Error) => void) { | 2985 | 2985 | // so we can tell if any async act() calls try to run in parallel. | 2986 | 2986 |
| 2987 | 2987 | let actingUpdatesScopeDepth = 0; | 2988 | | -let didWarnAboutUsingActInProd = false; | 2989 | 2988 |
| 2990 | 2989 | export function act(callback: () => Thenable<mixed>): Thenable<void> { | 2991 | 2990 | if (!__DEV__) { | 2992 | | - if (didWarnAboutUsingActInProd === false) { | 2993 | | - didWarnAboutUsingActInProd = true; | 2994 | | - // eslint-disable-next-line react-internal/no-production-logging | 2995 | | - console.error( | 2996 | | - 'act(...) is not supported in production builds of React, and might not behave as expected.', | 2997 | | - ); | 2998 | | - } | | 2991 | + invariant( | | 2992 | + false, | | 2993 | + 'act(...) is not supported in production builds of React.', | | 2994 | + ); | 2999 | 2995 | } | 3000 | 2996 |
| 3001 | 2997 | const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactFiberHostContext-test.internal.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,7 @@ describe('ReactFiberHostContext', () => { | 26 | 26 | .DefaultEventPriority; | 27 | 27 | }); | 28 | 28 |
| | 29 | + // @gate __DEV__ | 29 | 30 | it('works with null host context', async () => { | 30 | 31 | let creates = 0; | 31 | 32 | const Renderer = ReactFiberReconciler({ | @@ -83,6 +84,7 @@ describe('ReactFiberHostContext', () => { | 83 | 84 | expect(creates).toBe(2); | 84 | 85 | }); | 85 | 86 |
| | 87 | + // @gate __DEV__ | 86 | 88 | it('should send the context to prepareForCommit and resetAfterCommit', () => { | 87 | 89 | const rootContext = {}; | 88 | 90 | const Renderer = ReactFiberReconciler({ | |
|