System tests for TurboStream'd app without `sleep 1`

It depends on the tools used in your system test framework.

If using Capybara, you would just look for an element that you expect to be available after the turbo stream completes. Capybara waits until 1) it finds the element on the page or 2) Capybara times out. I believe the default timeout for Capybara is 2 seconds, which you can change using:

# spec_helper.rb
Capybara.default_wait_time = 10

There are other Capybara techniques (like one discussed on Stack Overflow
), but I’m not sure you need them with the right timeouts and selectors.

def wait_for_turbo(timeout = nil)
  if has_css?('.turbo-progress-bar', visible: true, wait: (0.25).seconds)
    has_no_css?('.turbo-progress-bar', wait: timeout.presence || 5.seconds)
  end
end

def wait_for_turbo_frame(selector = 'turbo-frame', timeout = nil)
  if has_selector?("#{selector}[busy]", visible: true, wait: (0.25).seconds)
    has_no_selector?("#{selector}[busy]", wait: timeout.presence || 5.seconds)
  end
end

FYI, I have not tested any of these methods as I’ve never needed to.

1 Like