Search code examples
javascriptruby-on-rails-3rspeccapybaraguard

Guard Executing all specs causes Capybara + JS test to return: 'Rack application timed out during boot'


I've got a Capybara script with Rsped that includes :js => true, script that works fine when I execute the script in isolation. Yay! Here's the script:

# spec/requests/capybara_and_js_spec.rb
require 'spec_helper'

describe "Associating Articles with Menus" do
  it "should include javascript", js: true do
    visit root_path
    page.should have_selector('script')
  end
end

When I execute the script I get:

.
Finished in 4.22 seconds
1 example, 0 failures

However, when I execute the same script with all of my specs via Guard Run all, I get this (I've omitted a couple thousand tests)

........................*...*..............Rack application timed out during boot
Rack application timed out during boot
F.....................................

Solution

  • I've spend quite a bit of time researching this issue, and found some interesting blog posts on the issue, but none of the solutions worked for me.

    Here are the options I tried:

    I switched from the default js driver for Capybara Selenium to Webkit and Poltergeist, like so:

    # Gemfile
    gem "capybara-webkit"
    
    # spec/spec_helper.rb
    Spork.prefork do
      Capybara.javascript_driver = :webkit
    end
    

    and

    # Gemfile
      gem "poltergeist"
    
    # spec/spec_helper.rb
    Spork.prefork do
      require 'capybara/poltergeist'
      Capybara.javascript_driver = :poltergeist
    end
    

    but no luck with either.

    Per this thread and this article I tried:

    # spec/spec_helper.rb
    Spork.prefork do
      Capybara.server_boot_timeout = 600  # Default is 10 my entire suite
    end                                   # takes ~550s to run, that's why I
                                          # attempted such a large boot timeout in
                                          # case the time was from beginning of suite 
                                          # execution.
    

    To no avail.

    Then I found this article, so I attempted:

    # spec/spec_helper.rb
    # initial advice was for cucumber, and thus recommended this to be placed in
    # the features/env.rb file
    def find_available_port
      server = TCPServer.new('127.0.0.1', 0)
      server.addr[1]
    ensure
      server.close if server
    end
    
    if ENV['TDDIUM'] then
      Capybara.server_port = find_available_port
    end
    

    But no luck with that.

    I also checked my database_cleaner settings to ensure DatabaseCleaner was playing nicely with my factories from FactoryGirl, per this issue on StackOverflow.

    Still no luck.

    Next I tried to parse out my capybara tests from my lower level rspec tests in my Guardfile, like so:

    group 'integration tests' do
      # Capybara Tests
      guard 'rspec', spec_paths: ['spec/requests'] do
        watch(%r{^spec/requests/.+_spec\.rb})
      end  
      # Cucumber Feature Tests
      guard 'cucumber', bundler: true do
        watch(%r{^features/.+\.feature$})
      end
    end
    
    group 'unit tests' do
      rspec_paths = ['spec/controllers', 'spec/helpers', 'spec/models', 'spec/views']
      # RSpec Unit Tests
      guard 'rspec', spec_paths: rspec_paths do
        watch(%r{^spec/.+_spec\.rb$})
      end
      # Jasmine JS Unit Tests
      guard 'jasmine', all_on_start: false, all_after_pass: false do
        watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
      end
    end
    

    and SUCCESS! Finally!