Skip to content

Fix flaky organization_spec.rb and donnation_summary_spec.rb specs#5521

Open
stefannibrasil wants to merge 6 commits intorubyforgood:mainfrom
hexdevs:sb-flaky-specs-5515
Open

Fix flaky organization_spec.rb and donnation_summary_spec.rb specs#5521
stefannibrasil wants to merge 6 commits intorubyforgood:mainfrom
hexdevs:sb-flaky-specs-5515

Conversation

@stefannibrasil
Copy link
Contributor

@stefannibrasil stefannibrasil commented Mar 20, 2026

Resolves #5515

Description

Testing against time related scenarios can lead to flakiness. I couldn't reproduce the failures locally but based on my previous experience of fixing flaky tests, I:

  • changed code and tests to use DateTime.current instead of DateTime.now. Rails extends the Time and DateTime objects, and includes the `current`` property for retrieving the time the Rails environment is set to (default = UTC), as opposed to the server time (could be anything). See https://stackoverflow.com/a/18811305
  • updated tests and factories to use the same DateTime helper as the model does
  • Wrap entire tests in a freeze_time block to ensure the dates are frozen when asserting against them.
  • I removed extra organization created factories to reuse the existing one

Type of change

  • Flaky test fix

How Has This Been Tested?

I could not reproduce the failure at all locally. All tests pass locally. But I believe this should help CI get more stable.

Rails extends the Time and DateTime objects, and
includes the `current`` property for retrieving the
time the Rails environment is set to
(default = UTC), as opposed to the server time
(could be anything).

https://stackoverflow.com/a/18811305
Freezing the time and checking for the same
time period should fix the flakiness.
Time was being frozen after creating the factory.
Move the test setup inside a freeze_time block
to ensure created_at is created 1.day.ago.

Plus, using the same date/time helper as the
IssuedAt concern helps with keeping the test
consistent with the code.
@stefannibrasil stefannibrasil marked this pull request as ready for review March 20, 2026 22:15
@stefannibrasil
Copy link
Contributor Author

@dorner could you approve the workflows so I can run this a few times in CI? 🙏🏼

@stefannibrasil stefannibrasil changed the title [WIP] Flaky specs Fix flaky organization_spec.rb and donnation_summary_spec.rb specs Mar 20, 2026
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.

Fix flaky / incorrect tests

1 participant