Webpack with Rails,
Rake vs RSpec
Warning: This post is very old. It may be out of date, and links in it may not work. Continue at your own risk.
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/spec_helper.rb into a
and change all your feature specs to require this file instead. My
spec/feature_helper.rb looks something like this:
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
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
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.
It should go without saying that you must replace
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.
I provide technical leadership, risk assessment, project planning, training, and additional development to software teams. If you need help taking a project from idea through execution, get in touch.