Skip to content

fix(test): wrap flaky homepage tests in travel_to blocks#2575

Merged
mroderick merged 1 commit intocodebar:masterfrom
mroderick:fix/flaky-homepage-test
Apr 16, 2026
Merged

fix(test): wrap flaky homepage tests in travel_to blocks#2575
mroderick merged 1 commit intocodebar:masterfrom
mroderick:fix/flaky-homepage-test

Conversation

@mroderick
Copy link
Copy Markdown
Collaborator

Summary

Fixes a flaky test failure in spec/features/visiting_homepage_spec.rb that was causing intermittent CI failures.

Problem

The test scenario "i can view the next 5 upcoming events" was failing intermittently in CI with the following error:

expected to find text "Rerum sed quia assumenda. at Ghislaine Treutel DO" in "..."

The events were not appearing on the homepage because they were being filtered out by the upcoming scope.

Root Cause

This is a time synchronization issue between fabricator timestamps and database scope queries:

  1. Fabricator timestamps are evaluated when the file loads:

    # spec/fabricators/event_fabricator.rb
    date_and_time Time.zone.now + 2.days  # Evaluated at FILE LOAD time
  2. The upcoming scope compares against Time.zone.now at query time:

    # app/models/concerns/listable.rb
    scope :upcoming, -> { where('date_and_time >= ?', Time.zone.now) }
  3. The controller's all_events method uses the upcoming scope:

    # app/controllers/dashboard_controller.rb
    events = Event.upcoming.take(DEFAULT_UPCOMING_EVENTS)

When time passes between fabricator file loading and test execution, events created with Time.zone.now + 2.days at file load time may actually be in the past relative to Time.zone.now at query time, causing them to be filtered out.

Solution

Wrap time-dependent test scenarios in travel_to(Time.current) blocks to freeze time:

scenario 'i can view the next 5 upcoming events' do
  travel_to(Time.current) do
    events.take(5).each { |event| expect(page).to have_content "#{event.name} at #{event.venue.name}" }
  end
end

This ensures both the fabricator timestamps and the scope queries use the same time reference.

Related Work

This follows the same pattern as commit 3f44835e ("fix(test): freeze time in tests to prevent flaky failures") which resolved similar flaky tests in 15 other files:

  • spec/features/listing_events_spec.rb
  • spec/features/chapter_spec.rb
  • spec/models/concerns/listable_spec.rb
  • And 12 others...

The visiting_homepage_spec.rb file was missed in that earlier fix.

Files Changed

  • spec/features/visiting_homepage_spec.rb - Added travel_to blocks around time-dependent scenarios

Testing

  • Test now consistently passes when time is frozen
  • Follows established pattern from previous flaky test fixes
  • No production code changes

References

Fix time-dependent flaky tests by wrapping scenarios in travel_to blocks.

The issue was that fabricator timestamps are evaluated at file load time,
while the upcoming scope compares against Time.zone.now at query time.
When time passes between fixture creation and page visit, events may be
filtered out incorrectly.

This follows the same pattern as commit 3f44835 which fixed similar
flaky tests in other files.
@mroderick mroderick merged commit e5b8443 into codebar:master Apr 16, 2026
13 of 15 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