Skip to content

Instantly share code, notes, and snippets.

@jarednorman
Created February 22, 2016 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarednorman/b94cf4e518e0f7c6aba2 to your computer and use it in GitHub Desktop.
Save jarednorman/b94cf4e518e0f7c6aba2 to your computer and use it in GitHub Desktop.
Webpack with Rails Part 3: Rake vs RSpec

Webpack with Rails Part 3: Rake vs RSpec

In part 2 we set up a Rake task to run webpack, ensuring that most deploy systems would build our entries for us.

I missed that we needed the webpack Rake task to be run before feature tests too. Without this, we need to run the task manually before running our test suite to avoid erroneous failures due to the assets not being up to date. Running Rake tasks from anywhere in your application in relatively easy, but we need a way to run webpack only if we're running feature specs. Fortunately, I always set my projects up to allow that anyway.

Separate all your feature spec specific code and configuration from your spec/rails_helper.rb/spec/spec_helper.rb into a spec/feature_helper.rb, and change all your feature specs to require this file instead. My spec/feature_helper.rb looks something like this:

# spec/feature_helper.rb

require 'rails_helper'
require 'capybara/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

It require's my rails_helper, which requires my spec_helper and sets up my Rails-specific test environment. Then it requires the gems that are needed for my feature tests, currently Capybara and Poltergeist (which are set as require: false in my Gemfile so they won't get loaded when I'm running specs that don't require them). Some projects might not have a rails_helper.rb, and that's fine; just require spec_helper.rb in your feature_helper.rb instead.

With this in place we can add RSpec configuration that will only get run if at least one feature spec is being run. Our problem calls for us to run our webpack Rake task before the test suite runs, so here's roughly what your helper file should like.

# spec/feature_helper.rb

require 'rails_helper'
require 'capybara/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  config.before(:suite) do
    YourApplicationName::Application.load_tasks
    Rake::Task[:webpack].invoke
  end
end

It should go without saying that you must replace YourApplicationName with the actual name of your Rails application.

We're done; when we run our test suite on your CI server or switch branches without the watcher running, they should run against up to date webpack entries.

Note: I tricked you into extracting your feature-specific configuration and requires into a separate file, and thus speeding up your individual test runs a tiny bit. You're welcome.

@zarkomilosevic
Copy link

zarkomilosevic commented Aug 10, 2017

This doesn`t work for me. I get error:


An error occurred in a `before(:suite)` hook.
Failure/Error: Rake::Task[:webpack].invoke

RuntimeError:
  Don't know how to build task 'webpack' (see --tasks)
# ./spec/feature_helper.rb:9:in `block (2 levels) in <top (required)>'
# -e:1:in `load'
# -e:1:in `<main>'

0 examples, 0 failures, 0 passed

Why i`m getting this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment