We've moved discussions to Discord

Recommended Configuration for Running Tests with Guardfile

Lee McAlilly
I usually set up rails to use Guard to run my tests when I change a file or so that I can just hit enter to run all of my tests, but that requires that I manually run bundle exec guard every time I start my rails server. My guardfile usually looks something like this:

# Use Gaurd to run tests as well
guard :minitest do
  # Run everything within 'test' if the test helper changes
  watch(%r{^test/test_helper\.rb$}) { 'test' }

  # Run everything within 'test/system' if ApplicationSystemTestCase changes
  watch(%r{^test/application_system_test_case\.rb$}) { 'test/system' }

  # Run the corresponding test anytime something within 'app' changes
  #   e.g. 'app/models/example.rb' => 'test/models/example_test.rb'
  watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }

  # Run a test any time it changes
  watch(%r{^test/.+_test\.rb$})

  # Run everything in or below 'test/controllers' everytime
  #   ApplicationController changes
  watch(%r{^app/controllers/application_controller\.rb$}) do
    'test/controllers'
  end

  # Run integration test every time a corresponding controller changes
  watch(%r{^app/controllers/(.+)_controller\.rb$}) do |m|
    "test/integration/#{m[1]}_test.rb"
  end

  # Run mailer tests when mailer views change
  watch(%r{^app/views/(.+)_mailer/.+}) do |m|
    "test/mailers/#{m[1]}_mailer_test.rb"
  end
end

But now that I've seen how guard "livereload" works, that's also a nice to have. Is there a way to configure both of these? I'm not sure how that would work if guard was a part of your Procfile the way it currently is with live reload, but wondering if I could configure my guardfile so that I can manually run bundle exec guard in a new terminal window for my tests (in addition to having foreman start it for the purposes of livereload.)

Does anyone have any recommendations on this approach? How would I do this in the Jumpstart guardfile, which by default looks like this:

guard "livereload" do
  extensions = {
    css: :css,
    scss: :css,
    sass: :css,
    js: :js,
    coffee: :js,
    html: :html,
    png: :png,
    gif: :gif,
    jpg: :jpg,
    jpeg: :jpeg
    # less: :less, # uncomment if you want LESS stylesheets done in browser
  }

  rails_view_exts = %w[erb haml slim]

  # file types LiveReload may optimize refresh for
  compiled_exts = extensions.values.uniq
  watch(%r{public/.+\.(#{compiled_exts * '|'})})

  extensions.each do |ext, type|
    watch(%r{
          (?:app|vendor)
          (?:/assets/\w+/(?<path>[^.]+) # path+base without extension
           (?<ext>\.#{ext})) # matching extension (must be first encountered)
          (?:\.\w+|$) # other extensions
          }x) do |m|
      path = m[1]
      "/assets/#{path}.#{type}"
    end
  end

  # file needing a full reload of the page anyway
  watch(%r{app/views/.+\.(#{rails_view_exts * '|'})$})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{config/locales/.+\.yml})
end
Lee McAlilly
Answer to this question, should anyone need it, is that you can use the Guard
"group" command
. I wrapped everything that comes with Jumpstart's Guardfile in a group block:
group :development do
end

And then added everything needed to run a my tests with Guard to a ":test" group.

Then, edited my Procfile to guard: bundle exec guard --group development. Now I can run bundle exec guard --group test in a new shell and it will run my tests while also running guard live reload in the terminal shell that I used to fire up the server with Foreman.
Matthias Orgler
Wow, thanks! It would be nice to have the tests set up to be run with guard by default in JSP though. Testing is such an important part of developing software (and especially Rails) – it should be a first class citizen in JSP as well.
Notifications
You’re not receiving notifications from this thread.