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
:js
test included in the body of tests being run?


--

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

We recently installed the
webpacker
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
spec/spec_helper.rb
:

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


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
:js
tests when developing with
guard
as they take too long. We just run the
:js
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
before(:suite)
.

Answer Source

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

  ENV[ 'ASSET_PRECOMPILE_DONE' ] = nil

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