Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 18: "missing act()" warnings partially missing if a prior render threw #21765

Closed
eps1lon opened this issue Jun 29, 2021 · 0 comments · Fixed by #21766
Closed

React 18: "missing act()" warnings partially missing if a prior render threw #21765

eps1lon opened this issue Jun 29, 2021 · 0 comments · Fixed by #21766
Labels
React 18 Bug reports, questions, and general feedback about React 18 Type: Discussion

Comments

@eps1lon
Copy link
Collaborator

eps1lon commented Jun 29, 2021

Specifically An update to * inside a test was not wrapped in act(...) is not logged when a prior update threw.

failing test (#21766)
// @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();
    }
    const [value, _setValue] = React.useState(defaultValue);
    setValue = _setValue;
    return value;
  }

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

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

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

What I noticed is that in

if (
warnsIfNotActing === true &&
executionContext === NoContext &&
ReactCurrentActQueue.current === null &&
// Our internal tests use a custom implementation of `act` that works by
// mocking the Scheduler package. Disable the `act` warning.
// TODO: Maybe the warning should be disabled by default, and then turned
// on at the testing frameworks layer? Instead of what we do now, which
// is check if a `jest` global is defined.
ReactCurrentActQueue.disableActWarning === false
) {

the executionContext will still equal RetryAfterError when we would expect a "missing act" warning.

"missing act" warnings are working as expected with legacy roots.

@eps1lon eps1lon added Type: Discussion React 18 Bug reports, questions, and general feedback about React 18 labels Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React 18 Bug reports, questions, and general feedback about React 18 Type: Discussion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant