fix(test): freeze time in tests to prevent flaky failures#2568
Merged
olleolleolle merged 1 commit intocodebar:masterfrom Apr 14, 2026
Merged
fix(test): freeze time in tests to prevent flaky failures#2568olleolleolle merged 1 commit intocodebar:masterfrom
olleolleolle merged 1 commit intocodebar:masterfrom
Conversation
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.
5e1ea55 to
3f44835
Compare
Collaborator
Author
|
@olleolleolle what do you think about removing TimeCop? Is that the way forwards for Rails projects? |
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) |
Collaborator
There was a problem hiding this comment.
This change is excellent and adds an implicit wait, which the previous form didn’t. That was also a source of flakes.
olleolleolle
approved these changes
Apr 14, 2026
Collaborator
olleolleolle
left a comment
There was a problem hiding this comment.
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: truecan 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
describeRSpec directive to wrap tests about a method.
👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Remove
timecopgem 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.nowevaluated 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 dependencyconfig/environments/test.rb- Remove timecop require and configspec/support/shared_examples/behaves_like_an_invitation.rb- Freeze time in invitation scope testsspec/models/concerns/listable_spec.rb- Replace Timecop with travel_to, add time freezingspec/models/attendance_warning_spec.rb- Freeze time in last_six_months testspec/features/*- Freeze time in feature tests with relative datesspec/presenters/*- Freeze time in presenter testsspec/lib/tasks/*- Freeze time in rake task testsTest Plan
travel_toovertimecopFixes flaky test failures in CI.