Skip to content

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

@eps1lon

Description

@eps1lon

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 https://github.com/facebook/react/blob/27c9c95e23ddedb9163373950e364dd62038f6c0/packages/react-reconciler/src/ReactFiberWorkLoop.new.js#L2855-L2865
the executionContext will still equal RetryAfterError when we would expect a "missing act" warning.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    React 18Bug reports, questions, and general feedback about React 18Type: Discussion

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions