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] Attempt to fix flaky unit tests #14583

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

LukasTy
Copy link
Member

@LukasTy LukasTy commented Sep 11, 2024

I suspect that the fake clock configuration might be leaking outside the intended scope and producing flaky unit tests (i.e. 1, 2, 3).

This PR aims to ensure that the timers mocking is reset after each describe block.
After more testing I concluded that manual clock restoring doesn't help at all as everything is already done.

I'm exploring the next best thing—avoid faking clock whenever possible and where needed, ensure that only Date is mocked to prevent running into cases where faked timeouts are bleeding into an async test.

The current solution in a nutshell:

  • Add @testing-library/user-event to Pickers and test packages dependencies to avoid the need of passing click and keyboard function down to utility functions
    • if you think that more verbose tests are a good compromise to avoid this dependency, I can refactor it.
      But we are already bringing in this dependency in test/performance-charts package... 🤷
  • Remove clock: 'fake' whenever possible
  • Use userEvent.click and userEvent.keyboard instead of the sync fireEvent utility

@LukasTy LukasTy added test core Infrastructure work going on behind the scenes labels Sep 11, 2024
@LukasTy LukasTy requested a review from a team September 11, 2024 14:01
@LukasTy LukasTy self-assigned this Sep 11, 2024
@mui-bot
Copy link

mui-bot commented Sep 11, 2024

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

Generated by 🚫 dangerJS against 91b61d3

@LukasTy LukasTy removed the request for review from a team September 11, 2024 14:21
@LukasTy LukasTy marked this pull request as draft September 11, 2024 14:23
@@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is generated after running pnpm start

@LukasTy LukasTy marked this pull request as ready for review September 11, 2024 20:17
@LukasTy LukasTy requested a review from a team September 11, 2024 20:17
@@ -86,6 +86,10 @@ describe('<DataGridPremium /> - Row grouping', () => {
describe('Setting grouping criteria', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I've tested that assumption in another PR and can conclude that the after block does not seem to get called. 🤔
I will test it more locally with the actual package, but adding a console.info in the after block does not log anything when running a test having a describe with withFakeTimers.

Copy link
Member Author

Choose a reason for hiding this comment

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

My assumption was not correct. The after hook seems to be called.
I'm currently clueless why explicitly adding the after hooks in the tests does seem to pass tests (ran them 3 times) and ensuring clock.restore is called in the original hook consistently doesn't help... 🤷
#14592

Copy link
Member

Choose a reason for hiding this comment

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

This is weird indeed.
Can you reproduce the flakiness locally? If so, please share some details, and I can look into it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't able to ever reproduce those flaky tests locally. 🙈
And I also reproduced flaky tests with on this PR. 💡

Copy link
Member Author

Choose a reason for hiding this comment

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

This agenda didn't produce any stable results.
I'm exploring a more long-term/stable solution. 😉

@JCQuintas
Copy link
Member

Btw, while working on #14508 I noticed there are two instances that makes the tests somewhat unstable, these clocks, and anything that changes moment.locale

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.

I don't understand how this works, but I don't want to block this PR if it reduces the flakiness 😅

Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 19, 2024
@cherniavskii cherniavskii mentioned this pull request Sep 19, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants