James Milani James Milani - 3 years ago 156
Ruby Question

Run rake assets:precompile before tests that include :js tag and skip otherwise

tl;dr --

Is it possible to run a single command (e.g.

rake assets:precompile
) only when there is a
test included in the body of tests being run?


I'm developing a rails 5, ruby 2.3.1 app that has a large
test suite.

We recently installed the
gem in the application which has caused us to have to run:

$ bundle exec rake assets:precompile

before running the tests. If the above command is not run the tests will be run against the most recently precompiled assets.

This has caused some headaches as developers have forgotten this step and then banged their heads against a wall until someone remembers to run that before running the test suite.

Ideally I'd like to simply add to the

config.before(:suite) do
system('bundle exec rake assets:precompile')

However, this will run the precompile before every single run of the test suite or any subset therein. This will drastically slow down development time for a backend dev that is simply trying to run a single request spec that normally takes 0.15 seconds.

Additionally, we normally skip running the
tests when developing with
as they take too long. We just run the
specs as a sanity check before deploying or pushing to a remote branch.

Unfortunately, adding the
{:js => true}
option to:

config.before(:suite, js:true)

doesn't work either, as options are ignored for the

Answer Source

Filter using :js tag and run once for all specs.


  config.before(:each, :js) do
      system 'bundle exec rake assets:precompile'
      ENV[ 'ASSET_PRECOMPILE_DONE' ] = 'true'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download