Skip to content

Commit

Permalink
Precompile assets before tests in test.rb so we get it early enough, …
Browse files Browse the repository at this point in the history
…and working for all tests
  • Loading branch information
DanielJackson-Oslo committed Jan 30, 2024
1 parent 3c3da43 commit ed0e3a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

# Simulate same asset settings as production
# This requires us to run yarn build, yarn build:css and rails assets:precompile before running tests
# This should be automatically done if you are running specs through parallel_spec or spec in rake
# This should be automatically done for you.
config.assets.prefix = "/assets-test"
config.assets.compile = false
config.serve_static_files = true

# Run precompilation before tests are run
system("rake precompile_assets_once")

# Settings specified here will take precedence over those in config/application.rb.

config.cache_classes = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ def precompile_assets_before(task_name)

task = Rake::Task[task_name]

task.enhance(["assets:precompile"])
task.enhance(["precompile_assets_once"])
end

# Enhance both spec and parallel:spec to precompile assets and clobber them after, to simulate prod
precompile_assets_before "spec"
# Enhance parallel:spec to precompile assets once, so it won't run per process
precompile_assets_before "parallel:spec"
13 changes: 13 additions & 0 deletions lib/tasks/precompile_assets_once.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

task "precompile_assets_once" => :environment do
next if ENV["TEST_ASSET_PRECOMPILE_DONE"]

p "Compiling assets..."
ENV["TEST_ASSET_PRECOMPILE_DONE"] = "true"
compilation = Rake::Task["assets:precompile"].invoke

abort "Pre-compilation failed. Cannot run tests." unless compilation

p "Assets compiled"
end

0 comments on commit ed0e3a5

Please sign in to comment.