Skip to content

Test with suspending component times out with fakeTimers: { enableGlobally: true } #1347

Closed as not planned
@robingullo

Description

@robingullo

Describe the bug

I've set up a simple test with a suspending component (no timers, just a return in async / await) that eventually renders a text. The test waits for the text with await findByText.
With jest.useFakeTimers(), the test pass.
With fakeTimers: { enableGlobally: true } in jest.config.js, the test fails.

Expected behavior

The test should pass in both cases.

Steps to Reproduce

  • Clone the examples/basic directory from the repo
  • Add this test:
import * as React from 'react';
import { render, screen } from '@testing-library/react-native';
import { Text, View } from 'react-native';

beforeEach(() => {
  jest.useFakeTimers();

  // Logs a warning if modern fake timers are not set
  jest.setSystemTime(Date.now());
});

test('should work', async () => {
  let loading = true;
  let data: string | undefined = undefined;

  const Suspending = () => {
    if (loading)
      throw (async () => {
        return await 'result';
      })().then((result) => {
        data = result;
        loading = false;
      });
    return <Text>{data}</Text>;
  };

  render(
    <View>
      <React.Suspense fallback={<Text>Loading</Text>}>
        <Suspending />
      </React.Suspense>
    </View>
  );

  await screen.findByText('result');
});
  • Run the test: it passes ✅
  • In jest.config.js, add the line fakeTimers: { enableGlobally: true }
  • Run the test: it fails ❌

Versions

npmPackages:
@testing-library/react-native: ^11.4.0 => 11.5.2
react: 18.1.0 => 18.1.0
react-native: 0.70.5 => 0.70.5
react-test-renderer: 18.1.0 => 18.1.0

Note:
I have the same result with RN 0.71.3 and the standard 'react-native' preset.

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