Skip to content

Unable to test components that suspend when using React 19 #1787

Open
@winghouchan

Description

@winghouchan

Describe the bug

When using React 19 and testing a component that suspends, the component never reaches its un-suspended state and the test fails with the following error:

  console.error
    A component suspended inside an `act` scope, but the `act` call was not awaited. When testing React components that depend on asynchronous data, you must await the result:
    
    await act(() => ...)

      at node_modules/react/cjs/react.development.js:1157:23

Expected behavior

The component should be able to un-suspend, allowing the test to run properly and pass.

Steps to Reproduce

After cloning the reproduction repository (a fork with a test case added) and installing dependencies, run the tests (yarn run test).

Relevant code (diff with upstream):

import * as React from 'react';
import { Pressable, Text, TextInput, View } from 'react-native';

// ...

import { fireEvent, render, screen } from '..';

// ...

test('supports components which can suspend', async () => {
  function wait(delay: number) {
    return new Promise<void>((resolve) => setTimeout(() => resolve(), delay));
  }

  function Suspendable<T>({ promise }: { promise: Promise<T> }) {
    React.use(promise);
    return <View testID="test" />;
  }

  function Fallback() {
    return <View testID="fallback" />;
  }

  render(
    <View>
      <React.Suspense fallback={<Fallback />}>
        <Suspendable promise={wait(1000)} />
      </React.Suspense>
    </View>,
  );

  expect(screen.getByTestId('fallback')).toBeOnTheScreen();
  expect(await screen.findByTestId('test')).toBeOnTheScreen();
});

Versions

@testing-library/react-native: ^13.2.0 => 13.2.0 
react: 19.0.0 => 19.0.0 
react-native: ^0.79.0 => 0.79.5 
react-test-renderer: 19.0.0 => 19.0.0 

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