Skip to content

React.StrictMode not working when child of wrapper in React 19 #1388

Description

@bernardobelchior
  • @testing-library/react version: 16.2.0
  • Testing Framework and version: Vitest v3.0.9
  • DOM Environment: JSDOM v26

Relevant code or config:

The assertion in the following snippets fails in React 19, but succeeds in React 18:

function Wrapper({ children }) {
  return <React.StrictMode>{children}</React.StrictMode>;
}

test("renders in strict mode when using a wrapper", async () => {
  let mounted = 0;
  function Component() {
    React.useEffect(() => {
      mounted += 1;
    });

    return null;
  }

  render(<Component />, { wrapper: Wrapper });

  expect(mounted).toEqual(2);
});
● › renders in strict mode when using a wrapper
expect(received).toEqual(expected) // deep equality

Expected: 2
Received: 1

  18 |   render(<Component />, { wrapper: Wrapper });
> 20 |   expect(mounted).toEqual(2);
     |                   ^
  21 | });

What you did:

Rendered StrictMode in Wrapper component that is passed do the wrapper property of the options object in the render function from React Testing Library.

What happened:

Expected Strict Mode to be enabled, but it wasn't. This can be seen because in Strict Mode in React 18 and 19 useEffect should be run twice, but in this case it was only run once.

This issue only happens in React 19. In React 18 it works as expected.

Reproduction:

React 19 - tests fail

React 18 - tests pass

Problem description:

In React 19, when rendering React.StrictMode in a component provided to wrapper, Strict Mode isn't enabled.

In React 18, however, Strict Mode is enabled.

Suggested solution:

In React 19, rendering React.StrictMode in a component provided to wrapper should enable Strict Mode.

Activity

  1. eps1lon commented on Apr 2, 2025

    @eps1lon
    Member

    This is intended React behavior: react/react#32315 (comment)

    You can use configure({ reactStrictMode: true }) (https://testing-library.com/docs/react-testing-library/api#reactstrictmode) to work around this.

  2. Janpot commented on Apr 2, 2025

    @Janpot

    @eps1lon Would it be possible to add a reactStrictMode option to the render() options as well so that it can be enabled on a per-test basis?

  3. eps1lon commented on Apr 2, 2025

    @eps1lon
    Member

    Yep, that's what I would recommend anyway. Can you work on a PR?

  4. bernardobelchior commented on Apr 2, 2025

    @bernardobelchior
    ContributorAuthor

    I can take a look at it

  5. bernardobelchior commented on Apr 2, 2025

    @bernardobelchior
    ContributorAuthor

    Here it is: #1390

  6. eps1lon commented on Apr 2, 2025

    @eps1lon
    Member

    Fixed in #1390

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions