Skip to content

fix(test): freeze time in tests to prevent flaky failures#2568

Open
mroderick wants to merge 1 commit intocodebar:masterfrom
mroderick:fix/time-dependent-flaky-tests
Open

fix(test): freeze time in tests to prevent flaky failures#2568
mroderick wants to merge 1 commit intocodebar:masterfrom
mroderick:fix/time-dependent-flaky-tests

Conversation

@mroderick
Copy link
Copy Markdown
Collaborator

@mroderick mroderick commented Apr 11, 2026

Summary

Remove timecop gem dependency and fix flaky tests caused by time-based comparisons without freezing time.

Problem

CI was failing due to flaky tests. The root cause: time-based scopes compare against Time.zone.now evaluated at query time, while test fixtures use relative times (2.days.ago, 1.week.from_now) evaluated at creation time. Any time gap between fixture creation and query execution could cause incorrect results.

Solution

Wrap time-dependent tests in travel_to(Time.current) blocks, ensuring all time calculations reference the same frozen moment.

Files Changed

  • Gemfile, Gemfile.lock - Remove timecop dependency
  • config/environments/test.rb - Remove timecop require and config
  • spec/support/shared_examples/behaves_like_an_invitation.rb - Freeze time in invitation scope tests
  • spec/models/concerns/listable_spec.rb - Replace Timecop with travel_to, add time freezing
  • spec/models/attendance_warning_spec.rb - Freeze time in last_six_months test
  • spec/features/* - Freeze time in feature tests with relative dates
  • spec/presenters/* - Freeze time in presenter tests
  • spec/lib/tasks/* - Freeze time in rake task tests

Test Plan

  • 508 non-feature tests pass (1 unrelated flaky test)
  • All time-dependent tests verified with multiple seeds
  • Standardized on ActiveRecord travel_to over timecop

Fixes flaky test failures in CI.

Remove timecop gem dependency and use Rails' travel_to helper.

Time-based scopes compare against Time.zone.now evaluated at query time,
while test fixtures used relative times evaluated at creation time. This
caused flaky tests when any time passed between fixture creation and query.

Files changed:
- Remove timecop from Gemfile and test.rb config
- Add travel_to blocks to time-dependent tests in shared examples,
  listable_spec, attendance_warning_spec, feature tests, rake tasks,
  and presenter specs

Fixes flaky test failures in CI runs.
@mroderick mroderick force-pushed the fix/time-dependent-flaky-tests branch from 5e1ea55 to 3f44835 Compare April 11, 2026 16:22
@mroderick mroderick marked this pull request as ready for review April 11, 2026 16:34
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.

1 participant