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

[test] Fix flaky tests #16257

Merged
merged 2 commits into from
Jan 20, 2025
Merged

[test] Fix flaky tests #16257

merged 2 commits into from
Jan 20, 2025

Conversation

lauri865
Copy link
Contributor

@lauri865 lauri865 commented Jan 19, 2025

Fixes flaky tests due to delayed side-effects and animations that run in different cycles in CI due to constrained environments.

Removes some dead code (according to the comments and my testing).

Ref discussion in #16231

Side note: if someone knows how to fix global instances of momentjs being reused for tests, then most tests would work in parallel, and that would speed up (at least local) testing significantly.

pnpm test:unit takes 210s
pnpm test:unit --parallel takes <25s

@mui-bot
Copy link

mui-bot commented Jan 19, 2025

Deploy preview: https://deploy-preview-16257--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against d7c5b5f

@LukasTy LukasTy added test component: data grid This is the name of the generic UI component, not the React module! component: pickers This is the name of the generic UI component, not the React module! labels Jan 20, 2025
@LukasTy LukasTy changed the title [tests] fix flaky tests (data grid & datepickers) [test] fix flaky tests (data grid & datepickers) Jan 20, 2025
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

Thank you for your time! 🙏
These are nice discoveries. 👍

Changes for Pickers make sense, but I can't vouch for Data Grid. 🙈
Besides, I feel that the PR title does not correctly portray the actual changes. 🤷

packages/x-data-grid/src/utils/isJSDOM.ts Show resolved Hide resolved
<LocalizationProvider dateAdapter={availableAdapters[adapterName]}>
<LocalizationProvider
dateAdapter={availableAdapters[adapterName]}
adapterLocale={adapterName === 'date-fns' ? enUS : 'en-US'}
Copy link
Member

Choose a reason for hiding this comment

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

Was it needed here, because of leaking config from the other test in the suite? 🤔
By default, we shouldn't need to explicitly set the enUS locale as it is the default if none is defined...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. Try running the test in isolation, it will fail 100% of the times with moment.js. It's specific to moment.js though, and due to importing side effects that seem to manipulate the locale of the global moment.js instance
import 'moment/locale/fr'; <- this as the last import seems to change the default locale

Maybe we could trigger locale change on top-level for momentjs adapter instead. Or if you know how to fix scoping for momentjs, that would be even better.

Copy link
Contributor Author

@lauri865 lauri865 Jan 20, 2025

Choose a reason for hiding this comment

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

It only seems to currently work in CI (and when you run the whole test suite) due to other tests changing the locale back to english. But I have seen the test fail in CI as well, I guess if this test happens to run before the other ones.

@@ -249,7 +249,7 @@ const PickersCalendarHeader = React.forwardRef(function PickersCalendarHeader(
</SwitchViewButton>
)}
</PickersCalendarHeaderLabelContainer>
<Fade in={view === 'day'}>
<Fade in={view === 'day'} appear={!reduceAnimations} enter={!reduceAnimations}>
Copy link
Member

Choose a reason for hiding this comment

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

Nice find. 👍
But it looks like there is still a hide/show animation when reduceAnimations is specified. 🤔
Does it really remove timeouts?
Have you considered going a similar route as PickersFadeTransitionGroup, where it renders a React.Fragment wrapper when reduceAnimations is true? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That'd probably the correct way, but I tried to go the easy route to prove the root cause. There's an exit prop as well that should remove the hide animations. Even better might be to put reduceAnimations in a context and have wrappers for all animation components.

Copy link
Member

Choose a reason for hiding this comment

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

That'd probably the correct way, but I tried to go the easy route to prove the root cause.

Gotcha. 👌

There's an exit prop as well that should remove the hide animations.

I tried locally, but it does not seem to change anything, the animation is still there.
If we are going for pure "reduce animations", then there should be no animations when it is toggled, as this prop is mainly an "accessibility" setting.

Even better might be to put reduceAnimations in a context and have wrappers for all animation components.

Great point, now that we have this context, we should be able to move this setting there. 👍

…teCalendar.test.tsx

Co-authored-by: Lukas Tyla <[email protected]>
Signed-off-by: Lauri <[email protected]>
@@ -0,0 +1,2 @@
export const isJSDOM =
Copy link
Member

Choose a reason for hiding this comment

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

I would keep the try/catch here as it was in useGridVirtualScroller so it doesn't fail in server env

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would it fail in server environment? Besides window not being defined, which it checks for.

Copy link
Member

Choose a reason for hiding this comment

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

I didn't notice there was a check for window alread 👍🏻

@cherniavskii cherniavskii added needs cherry-pick The PR should be cherry-picked to master after merge v7.x labels Jan 20, 2025
Copy link
Member

@cherniavskii cherniavskii left a comment

Choose a reason for hiding this comment

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

Thank you!

@cherniavskii cherniavskii requested a review from LukasTy January 20, 2025 12:44
@LukasTy LukasTy changed the title [test] fix flaky tests (data grid & datepickers) [test] Fix flaky tests Jan 20, 2025
@LukasTy LukasTy merged commit 085befa into mui:master Jan 20, 2025
25 of 26 checks passed
Copy link

Cherry-pick PRs will be created targeting branches: v7.x

github-actions bot pushed a commit that referenced this pull request Jan 20, 2025
Signed-off-by: Lauri <[email protected]>
Co-authored-by: Lukas Tyla <[email protected]>
@LukasTy
Copy link
Member

LukasTy commented Jan 20, 2025

Well, some tests on karma are still flaky. 🙈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! component: pickers This is the name of the generic UI component, not the React module! needs cherry-pick The PR should be cherry-picked to master after merge test v7.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants