I’m currently building a Rails 7 app that was started with
--css flag provided by Rails is a fantastic improvement over previous implementations like Webpacker. Now we can just run
bin/dev to start up our local development server and have it watch for changed files so that it can recompile the assets if needed.
That’s a good start, but what about testing? System tests, specifically, will need to interact with a current compilation of our assets. You shouldn’t be relegated to firing up a local development environment to get things compiled and accurate for your tests as you change your code. I’m sure there are several ways to solve this, but here’s what I’m doing.
Compile Before the Tests Run
I’m using rspec-rails for my tests, but the gist of this post should work with whatever you’re using. In
rails_helper.rb I add a little check that runs the nifty
rails test:prepare to get everything compiled.
# rails_helper.rb RSpec.configure do |config| # Precompile the assets (used by system tests) config.before :all do if !ENV["ASSET_PRECOMPILE_DONE"] prep_passed = system "rails test:prepare" ENV["ASSET_PRECOMPILE_DONE"] = "true" abort "\nYour assets didn't compile. Exiting WITHOUT running any tests. Review the output above to resolve any errors." if !prep_passed end end end
system command returns true or false depending on whether asset compilation succeeds. If the compilation fails, we
abort and skip running the tests. If things are successful, we continue with the tests while setting an environment variable that stops the compilation from running every time.