Skip to content

Commit

Permalink
Codemod redundant async act scopes (#26350)
Browse files Browse the repository at this point in the history
Prior to #26347, our internal `act` API (not the public API) behaved
differently depending on whether the scope function returned a promise
(i.e. was an async function), for historical reasons that no longer
apply. Now that this is fixed, I've codemodded all async act scopes that
don't contain an await to be sync.

No pressing motivation other than it looks nicer and the codemod was
easy. Might help avoid confusion for new contributors who see async act
scopes with nothing async inside and infer it must be like that for a
reason.
  • Loading branch information
acdlite authored Mar 8, 2023
1 parent 0373782 commit 62cd5af
Show file tree
Hide file tree
Showing 78 changed files with 1,277 additions and 1,285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('ReactInternalTestUtils', () => {
}

const root = ReactNoop.createRoot();
await act(async () => {
await act(() => {
root.render(<App />);
});
assertLog(['A', 'B', 'C']);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('ReactFlight', () => {
return use(promise);
}

await act(async () => {
await act(() => {
startTransition(() => {
ReactNoop.render(
<>
Expand Down Expand Up @@ -554,7 +554,7 @@ describe('ReactFlight', () => {
return use(promise);
}

await act(async () => {
await act(() => {
startTransition(() => {
ReactNoop.render(
<NoErrorExpected>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('ReactHooksInspectionIntegration', () => {
const {onMouseDown: setStateA, onMouseUp: setStateB} =
renderer.root.findByType('div').props;

await act(async () => setStateA('Hi'));
await act(() => setStateA('Hi'));

childFiber = renderer.root.findByType(Foo)._currentFiber();
tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
Expand All @@ -83,7 +83,7 @@ describe('ReactHooksInspectionIntegration', () => {
},
]);

await act(async () => setStateB('world!'));
await act(() => setStateB('world!'));

childFiber = renderer.root.findByType(Foo)._currentFiber();
tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('ReactHooksInspectionIntegration', () => {
);
}
let renderer;
await act(async () => {
await act(() => {
renderer = ReactTestRenderer.create(<Foo prop="prop" />);
});

Expand Down Expand Up @@ -203,7 +203,7 @@ describe('ReactHooksInspectionIntegration', () => {
},
]);

await act(async () => {
await act(() => {
updateStates();
});

Expand Down Expand Up @@ -301,7 +301,7 @@ describe('ReactHooksInspectionIntegration', () => {
);
}
let renderer;
await act(async () => {
await act(() => {
renderer = ReactTestRenderer.create(<Foo prop="prop" />);
});

Expand Down Expand Up @@ -370,7 +370,7 @@ describe('ReactHooksInspectionIntegration', () => {
},
]);

await act(async () => {
await act(() => {
updateStates();
});

Expand Down Expand Up @@ -984,7 +984,7 @@ describe('ReactHooksInspectionIntegration', () => {
children: ['count: ', '1'],
});

await act(async () => incrementCount());
await act(() => incrementCount());
expect(renderer.toJSON()).toEqual({
type: 'div',
props: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ describe('ReactDOMConsoleErrorReporting', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(<Foo />);
});

await act(async () => {
await act(() => {
container.firstChild.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand All @@ -247,7 +247,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -384,7 +384,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand All @@ -405,7 +405,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -525,7 +525,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand All @@ -546,7 +546,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -597,7 +597,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
root.render(<NoError />);
});
expect(container.textContent).toBe('OK');
Expand All @@ -623,11 +623,11 @@ describe('ReactDOMConsoleErrorReporting', () => {
);
}

await act(async () => {
await act(() => {
ReactDOM.render(<Foo />, container);
});

await act(async () => {
await act(() => {
container.firstChild.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
Expand Down Expand Up @@ -698,7 +698,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -765,7 +765,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand All @@ -784,7 +784,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
throw Error('Boom');
}

await act(async () => {
await act(() => {
ReactDOM.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -837,7 +837,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -907,7 +907,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand All @@ -929,7 +929,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
return null;
}

await act(async () => {
await act(() => {
ReactDOM.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -982,7 +982,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand Down Expand Up @@ -1053,7 +1053,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand All @@ -1075,7 +1075,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
return null;
}

await act(async () => {
await act(() => {
ReactDOM.render(
<ErrorBoundary>
<Foo />
Expand Down Expand Up @@ -1128,7 +1128,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
// Check next render doesn't throw.
windowOnError.mockReset();
console.error.mockReset();
await act(async () => {
await act(() => {
ReactDOM.render(<NoError />, container);
});
expect(container.textContent).toBe('OK');
Expand Down
16 changes: 8 additions & 8 deletions packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(<Form />);
});

Expand Down Expand Up @@ -474,7 +474,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(<Form />);
});

Expand All @@ -484,7 +484,7 @@ describe('ReactDOMFiberAsync', () => {
// Dispatch a click event on the Disable-button.
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
await act(async () => {
await act(() => {
disableButton.dispatchEvent(firstEvent);
});

Expand All @@ -498,7 +498,7 @@ describe('ReactDOMFiberAsync', () => {
const secondEvent = document.createEvent('Event');
secondEvent.initEvent('click', true, true);
// This should force the pending update to flush which disables the submit button before the event is invoked.
await act(async () => {
await act(() => {
submitButton.dispatchEvent(secondEvent);
});

Expand Down Expand Up @@ -533,7 +533,7 @@ describe('ReactDOMFiberAsync', () => {
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
root.render(<Form />);
});

Expand All @@ -543,7 +543,7 @@ describe('ReactDOMFiberAsync', () => {
// Dispatch a click event on the Enable-button.
const firstEvent = document.createEvent('Event');
firstEvent.initEvent('click', true, true);
await act(async () => {
await act(() => {
enableButton.dispatchEvent(firstEvent);
});

Expand All @@ -557,7 +557,7 @@ describe('ReactDOMFiberAsync', () => {
const secondEvent = document.createEvent('Event');
secondEvent.initEvent('click', true, true);
// This should force the pending update to flush which enables the submit button before the event is invoked.
await act(async () => {
await act(() => {
submitButton.dispatchEvent(secondEvent);
});

Expand Down Expand Up @@ -643,7 +643,7 @@ describe('ReactDOMFiberAsync', () => {
}

const oldRoot = ReactDOMClient.createRoot(container);
await act(async () => {
await act(() => {
oldRoot.render(<OldApp />);
});

Expand Down
Loading

0 comments on commit 62cd5af

Please sign in to comment.