From 86c8c8db7938319027132eeea81c9b7e28938f9d Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 13 Feb 2023 21:45:59 +0100 Subject: [PATCH] test: Don't retry flushActWork if flushUntilNextPaint threw (#26121) ## Summary Fixes "ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down." in `ReactIncrementalErrorHandling-test.internal.js` Alternatives: 1. Additional `await act(cb)` call where `cb` makes sure we can flush until next paint without throwing ```js // Ensure test isn't exited with pending work await act(async () => { root.render(); }); ``` 1. Use `toFlushAndThrow` ```diff - let error; - try { - await act(async () => { - root.render(); - }); - } catch (e) { - error = e; - } + root.render(); - expect(error.message).toBe('Oops!'); + expect(Scheduler).toFlushAndThrow('Oops!'); expect(numberOfThrows < 100).toBe(true); ``` But then it still wouldn't make sense to pass `resolve` and `reject` to the next `flushActWork`. Even if the next `flushActWork` would flush until next paint without throwing, we couldn't resolve or reject because we already did reject. ## How did you test this change? - `yarn test --watch packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js` produces no more errors after the test finishes. --- packages/jest-react/src/internalAct.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-react/src/internalAct.js b/packages/jest-react/src/internalAct.js index 5d87891e7109e..da7e9f6108490 100644 --- a/packages/jest-react/src/internalAct.js +++ b/packages/jest-react/src/internalAct.js @@ -128,6 +128,7 @@ function flushActWork(resolve: () => void, reject: (error: any) => void) { Scheduler.unstable_flushUntilNextPaint(); } catch (error) { reject(error); + return; } // If Scheduler yields while there's still work, it's so that we can