From 6431b94abaecd4cba7ec9de8257c2e8539c837bf Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 23 Nov 2023 16:30:02 +1100 Subject: [PATCH 1/3] Remove unnecessary condition in spec helper This helper file is only loaded when system specs are loaded. So we don't need to check for the absence of system specs because this file wouldn't be loaded if there were not system specs to be run. --- spec/system/support/precompile_assets.rb | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/spec/system/support/precompile_assets.rb b/spec/system/support/precompile_assets.rb index cb137f19e6..2d8958c147 100644 --- a/spec/system/support/precompile_assets.rb +++ b/spec/system/support/precompile_assets.rb @@ -1,23 +1,12 @@ # frozen_string_literal: true RSpec.configure do |config| - # Skip assets precompilcation if we exclude system specs. - # For example, you can run all non-system tests via the following command: - # - # rspec --tag ~type:system - # - # In this case, we don't need to precompile assets. - next if - config.filter.opposite.rules[:type] == "system" || - config.exclude_pattern.match?(%r{spec/system}) - config.before(:suite) do # We can use webpack-dev-server for tests, too! # Useful if you working on a frontend code fixes and want to verify them via system tests. next if Webpacker.dev_server.running? $stdout.puts "\n Precompiling assets.\n" - Webpacker.compile end end From 37feefce37f91a28d400dd5161c3346bdea21042 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 2 Nov 2023 15:07:01 +1100 Subject: [PATCH 2/3] Avoid precompiling assets unnecessary We had two calls to precompile assets, one for all specs and then a second one just for system specs. The one for system specs is better because it checks for the webpack dev server before compiling assets. So let's use just that one. --- spec/spec_helper.rb | 6 ------ spec/{system => }/support/precompile_assets.rb | 0 2 files changed, 6 deletions(-) rename spec/{system => }/support/precompile_assets.rb (100%) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5422778d4a..ef36599bbe 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,12 +4,6 @@ require 'base_spec_helper' require 'database_cleaner' RSpec.configure do |config| - # Precompile Webpacker assets (once) when starting the suite. The default setup can result - # in the assets getting compiled many times throughout the build, slowing it down. - config.before :suite do - Webpacker.compile - end - # Fix encoding issue in Rails 5.0; allows passing empty arrays or hashes as params. config.before(:each, type: :controller) { @request.env["CONTENT_TYPE"] = 'application/json' } end diff --git a/spec/system/support/precompile_assets.rb b/spec/support/precompile_assets.rb similarity index 100% rename from spec/system/support/precompile_assets.rb rename to spec/support/precompile_assets.rb From 3586497c562dcf7ccffeeaa95ad61f78d11cca97 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 23 Nov 2023 16:58:20 +1100 Subject: [PATCH 3/3] Precompile assets only when needed --- spec/support/precompile_assets.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spec/support/precompile_assets.rb b/spec/support/precompile_assets.rb index 2d8958c147..762a1c27b7 100644 --- a/spec/support/precompile_assets.rb +++ b/spec/support/precompile_assets.rb @@ -6,7 +6,13 @@ RSpec.configure do |config| # Useful if you working on a frontend code fixes and want to verify them via system tests. next if Webpacker.dev_server.running? - $stdout.puts "\n Precompiling assets.\n" - Webpacker.compile + specs_needing_assets = %i[controller feature mailer request system view] + examples = RSpec.world.filtered_examples.values.flatten + types = examples.map(&:metadata).pluck(:type).uniq + + if types.intersect?(specs_needing_assets) + $stdout.puts "\n Precompiling assets.\n" + Webpacker.compile + end end end