Skip to content

Commit

Permalink
fix: restore execution context after RetryAfterError completed (#21766)
Browse files Browse the repository at this point in the history
* test: Add failing test due to executionContext not being restored

* fix: restore execution context after RetryAfterError completed

* Poke codesandbox/ci

* Completely restore executionContext

* expect a specific error
  • Loading branch information
eps1lon authored Jul 13, 2021
1 parent 9fec3f2 commit 9090257
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,33 @@ function runActTests(label, render, unmount, rerender) {
]);
});

// @gate __DEV__
it('warns if a setState is called outside of act(...) after a component threw', () => {
let setValue = null;
function App({defaultValue}) {
if (defaultValue === undefined) {
throw new Error('some error');
}
const [value, _setValue] = React.useState(defaultValue);
setValue = _setValue;
return value;
}

expect(() => {
act(() => {
render(<App defaultValue={undefined} />, container);
});
}).toThrow('some error');

act(() => {
rerender(<App defaultValue={0} />, container);
});

expect(() => setValue(1)).toErrorDev([
'An update to App inside a test was not wrapped in act(...).',
]);
});

describe('fake timers', () => {
beforeEach(() => {
jest.useFakeTimers();
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ function performConcurrentWorkOnRoot(root, didTimeout) {
: renderRootSync(root, lanes);
if (exitStatus !== RootIncomplete) {
if (exitStatus === RootErrored) {
const prevExecutionContext = executionContext;
executionContext |= RetryAfterError;

// If an error occurred during hydration,
Expand All @@ -800,6 +801,8 @@ function performConcurrentWorkOnRoot(root, didTimeout) {
lanes = errorRetryLanes;
exitStatus = renderRootSync(root, errorRetryLanes);
}

executionContext = prevExecutionContext;
}

if (exitStatus === RootFatalErrored) {
Expand Down Expand Up @@ -972,6 +975,7 @@ function performSyncWorkOnRoot(root) {

let exitStatus = renderRootSync(root, lanes);
if (root.tag !== LegacyRoot && exitStatus === RootErrored) {
const prevExecutionContext = executionContext;
executionContext |= RetryAfterError;

// If an error occurred during hydration,
Expand All @@ -993,6 +997,8 @@ function performSyncWorkOnRoot(root) {
lanes = errorRetryLanes;
exitStatus = renderRootSync(root, lanes);
}

executionContext = prevExecutionContext;
}

if (exitStatus === RootFatalErrored) {
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ function performConcurrentWorkOnRoot(root, didTimeout) {
: renderRootSync(root, lanes);
if (exitStatus !== RootIncomplete) {
if (exitStatus === RootErrored) {
const prevExecutionContext = executionContext;
executionContext |= RetryAfterError;

// If an error occurred during hydration,
Expand All @@ -800,6 +801,8 @@ function performConcurrentWorkOnRoot(root, didTimeout) {
lanes = errorRetryLanes;
exitStatus = renderRootSync(root, errorRetryLanes);
}

executionContext = prevExecutionContext;
}

if (exitStatus === RootFatalErrored) {
Expand Down Expand Up @@ -972,6 +975,7 @@ function performSyncWorkOnRoot(root) {

let exitStatus = renderRootSync(root, lanes);
if (root.tag !== LegacyRoot && exitStatus === RootErrored) {
const prevExecutionContext = executionContext;
executionContext |= RetryAfterError;

// If an error occurred during hydration,
Expand All @@ -993,6 +997,8 @@ function performSyncWorkOnRoot(root) {
lanes = errorRetryLanes;
exitStatus = renderRootSync(root, lanes);
}

executionContext = prevExecutionContext;
}

if (exitStatus === RootFatalErrored) {
Expand Down

0 comments on commit 9090257

Please sign in to comment.