Skip to content

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

Merged
olleolleolle merged 1 commit intocodebar:masterfrom
mroderick:fix/time-dependent-flaky-tests
Apr 14, 2026
Merged

fix(test): freeze time in tests to prevent flaky failures#2568
olleolleolle merged 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
@mroderick
Copy link
Copy Markdown
Collaborator Author

@olleolleolle what do you think about removing TimeCop? Is that the way forwards for Rails projects?

@olleolleolle
Copy link
Copy Markdown
Collaborator

@mroderick It’s nice to not have to have a special dependency if we get all we need from the built-in. If we can get away with not having Timecop, I say thank it and let it go.


expect(page).to have_content('Announcement successfully created')
expect(page.current_path).to eq(admin_announcements_path)
expect(page).to have_current_path(admin_announcements_path, ignore_query: true)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This change is excellent and adds an implicit wait, which the previous form didn’t. That was also a source of flakes.

Copy link
Copy Markdown
Collaborator

@olleolleolle olleolleolle left a comment

Choose a reason for hiding this comment

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

I can see that there were multiple kinds of fixes (I assume lint on save made a few of them).

Among fixes in here:

  • RSpec metadata foo: true can be just the Symbol :foo.
  • Use single quotes when possible (perhaps a low-value lint rule, but it’s there, so this harmonizes the codebase).
  • Use the describe RSpec directive to wrap tests about a method.

👍

@olleolleolle olleolleolle merged commit 07c5322 into codebar:master Apr 14, 2026
8 checks passed
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.

2 participants