Skip to content

[chore] Stabilize flaky tests#11121

Merged
djhi merged 5 commits intomasterfrom
fix-flaky-tests
Jan 19, 2026
Merged

[chore] Stabilize flaky tests#11121
djhi merged 5 commits intomasterfrom
fix-flaky-tests

Conversation

@WiXSL
Copy link
Copy Markdown
Collaborator

@WiXSL WiXSL commented Jan 17, 2026

Problem

Some tests are flaky.
Locally, these tests tend to fail regularly.

Solution

Improve the code to stabilize such tests.
Now these tests always pass locally as well.

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

expect(screen.queryByText('World')).not.toBeNull();
expect(screen.queryByText('mutating')).toBeNull();
},
{ timeout: 3000 }
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Increased waitFor timeout to cover the dataProvider’s 1000 ms delay; the assertion was right at the edge and could fail due to CI jitter

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We prefer to pass a custom dataProvider where we manually resolve the promises. example:

it('renders nothing while loading by default', async () => {
let resolveCheckAuth;
const authProvider: AuthProvider = {
login: () => Promise.reject('bad method'),
logout: () => Promise.reject('bad method'),
checkAuth: () => Promise.reject('bad method'),
checkError: () => Promise.reject('bad method'),
getPermissions: () => Promise.reject('bad method'),
canAccess: () =>
new Promise(resolve => {
resolveCheckAuth = resolve;
}),
};
const { container } = render(<Basic authProvider={authProvider} />);
expect(container.textContent).toEqual('');
expect(screen.queryByText('protected content')).toBeNull();
resolveCheckAuth(true);
await screen.findByText('protected content');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And we configured a large timeout globally in jest config and setup files. We should probably remove the explicit timeout as it may override the large default one

it('when optimistic, it accepts middlewares and displays error and error side effects when dataProvider promise rejects', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
render(<WithMiddlewaresErrorOptimistic timeout={10} />);
render(<WithMiddlewaresErrorOptimistic timeout={200} />);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Raised the optimistic story timeout so the success state is observable before the reject; the error previously landed too fast and intermittently broke the assert

await waitFor(() => {
// 9 because War and Peace is handled separately
expect(screen.queryAllByLabelText('Delete')).toHaveLength(9);
expect(screen.queryAllByLabelText('Delete').length).toBeGreaterThan(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Avoided asserting an exact count of delete buttons (pagination/render-dependent) and instead check that at least one appears; less brittle

await screen.findByText('9 items selected');
expect(screen.queryByRole('button', { name: 'Select all' })).toBeNull();
});
}, 10000);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Increased the timeout for the specific test; the flow depends on multiple renders and was sensitive to the default 5s limit in CI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same

});

it('should close the filter menu on removing all filters', async () => {
const user = userEvent.setup();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Switched from counting global checkboxes to asserting pagination text and used userEvent with await; avoids races from async renders and MUI portals

});

it('should not reapply previous filter form values when clearing nested AutocompleteArrayInput', async () => {
const user = userEvent.setup();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Select Autocomplete options via userEvent and keyboard navigation instead of matching raw text; avoids failures due to MUI portal/virtualized rendering

});
});

describe('"Select all" button', () => {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Synchronize on real content (‘War and Peace’) instead of checkbox counts and allow more time per test; reduces flakes from slow loads and DOM timing

Copy link
Copy Markdown
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

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

Neat!

screen.queryByRole('button', { name: 'Select all' })
).toBeNull();
});
}, 10000);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is probably not needed anymore with the new config, right? Same for the others

@djhi djhi added this to the 5.13.7 milestone Jan 19, 2026
@djhi djhi merged commit b9d6ff0 into master Jan 19, 2026
15 checks passed
@djhi djhi deleted the fix-flaky-tests branch January 19, 2026 16:50
@slax57 slax57 modified the milestones: 5.13.7, 5.14.0 Jan 23, 2026
@slax57 slax57 changed the title [Fix] Stabilize flaky tests [chore] Stabilize flaky tests Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RFR Ready For Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants