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

tests with waitFor or findBy only pass with concurrentRoot: false #1749

Open
gfreeman-karmak opened this issue Feb 25, 2025 · 1 comment
Open

Comments

@gfreeman-karmak
Copy link

gfreeman-karmak commented Feb 25, 2025

We're having an odd issue since upgrading to v13. We had a lot of tests suddenly break, and they were all tests that depended on waitFor, whether using it directly or using a findBy query. For some reason when the waitFor loop advances jest timers, the screen is no longer updating, so expectations and queries never resolve.

What I found fixed the issue for now is to set concurrentRoot: false, but that can only be temporary until react v19.

Interestingly, I also tried editing the wait-for.js code directly to wrap jest.advanceTimersByTime(interval); in act and that also worked. However it appears IS_REACT_ACT_ENVIRONMENT is purposely set to false, so I have to assume the omission of act is intentional, so I don't understand why concurrentRoot: true would suddenly break the tests.

I wrote a small test to illustrate:

import { afterEach, expect, jest, test } from '@jest/globals';
import { fireEvent, render, screen } from '@testing-library/react-native';
import { useEffect, useState } from 'react';
import { Pressable, View } from 'react-native';

jest.useFakeTimers();

const onPress = jest.fn();

function MockView() {
    const [buttonVisible, setButtonVisible] = useState(false);

    useEffect(() => {
        setTimeout(() => setButtonVisible(true), 100);
    }, []);

    return (
        <View>
            {buttonVisible && <Pressable accessibilityRole='button' onPress={onPress}>Button</Pressable>}
        </View>
    );
}

afterEach(() => {
    jest.clearAllMocks();
});

test.each([true, false])('button press, concurrentRoot: %s', async (concurrentRoot) => {
    render(<MockView />, { concurrentRoot });
    fireEvent.press(await screen.findByRole('button'));
    expect(onPress).toHaveBeenCalled();
});

The test with concurrentRoot: false passes with no issue, but in the concurrentRoot: true test, findBy never finds the button: Unable to find an element with role: button.

Any ideas?

react: 18.3.1
react-native: 0.77.0
react-test-renderer: 18.3.1
@testing-library/react-native: 13.0.1
jest: 29.7.0

Thanks

@flaviomsb
Copy link

I'm experiencing the same issue after the recent upgrade to react-native 0.77.1 and @testing-library/react-native: 13.0.1. I am also on react 18.3.1 and jest 29.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants