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

add possibility to configure interval with configure method #1507

Conversation

maciekstosio
Copy link

Summary

Currently, we're able to override the default timeout, but we can't do the same for the interval. For my test cases, 50ms has proven to be too long of an interval, and changing that setting for each case is tedious. I added a possibility to configure the default interval by configure method to solve that.

Test plan

I added tests based on those for interval and timeout.

@@ -552,7 +552,7 @@ Defined as:
```ts
function waitForElementToBeRemoved<T>(
expectation: () => T,
{ timeout: number = 4500, interval: number = 50 }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed like a mistake based on what is in code (?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is, thanks for fixing that!

@maciekstosio maciekstosio changed the title add possibility to configure interval with config method add possibility to configure interval with configure method Sep 24, 2023
Copy link
Collaborator

@pierrezimmermannbam pierrezimmermannbam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @maciekstosio! This looks good to me, you'll need another maintainer's approval before this can be merged.

I'm curious though, why do you think that the 50 ms interval is too long for you?

@maciekstosio
Copy link
Author

@pierrezimmermannbam I'm testing two screens, first one checks if the user is logged in, and it moves to another one that fetches data (and displays loader while fetch is in progress).

        render(
            <App
                tabsInitialRouteName='collection'
            />
        )
        
        await waitForElementToBeRemoved(() => screen.getByText('general.loading'))        
        await waitForElementToBeRemoved(() => screen.getByTestId('loading-indicator'))
        ...

Now, I had a problem that test failed on second waitForElementToBeRemoved when I was running multiple test cases, but succeeded when running alone. Fetch requests are mocked with nock. So I come up with an idea that mocked request can be just faster than 50ms and changing to 10ms helped. I assume it succeeded when running alone just because of some additional work on startup, but If you have some other ideas, I'm happy to discuss :)

@mdjastrzebski
Copy link
Member

@maciekstosio regarding your test case, you should wait only for the first element to be removed, as waitForElementToBeRemoved requires element to be initially present and subsequently removed. For your second control you should just use:

expect(screen.getByTestId('loading-indicator')).not.toBeOnTheScreen()

@mdjastrzebski
Copy link
Member

mdjastrzebski commented Sep 26, 2023

@maciekstosio I am somewhat hesitant on adding this option, as it seems that it's not really related to the root issue you are having. Relying on 10ms grain timing using real timers in test will probably result in flaky tests. The recommended solution would in such case would be to use fake timers, as they over more precise control.

Linking to similar DOM Testing Library issue.

@pierrezimmermannbam
Copy link
Collaborator

Oh ok I get it. I would agree with @mdjastrzebski that diminishing the interval for this use case may not be the optimal solution. Maybe for this particular use case, if you really want to test both loading states you could use the delay option from nock to ensure your API call doesn't resolve too early?

@maciekstosio
Copy link
Author

Yeah, you're right. I had a problem using fake timers, thus I didn't like that approach, but it seems like it was because of nock don't work with it properly. I changed to fetch-mock-jest and it works as expected. Thanks for your suggestions

@pierrezimmermannbam
Copy link
Collaborator

Alright I'll close that pr for now then

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

Successfully merging this pull request may close these issues.

None yet

3 participants