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/support/precompile_assets.rb b/spec/support/precompile_assets.rb new file mode 100644 index 0000000000..762a1c27b7 --- /dev/null +++ b/spec/support/precompile_assets.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + 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? + + 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 diff --git a/spec/system/support/precompile_assets.rb b/spec/system/support/precompile_assets.rb deleted file mode 100644 index cb137f19e6..0000000000 --- a/spec/system/support/precompile_assets.rb +++ /dev/null @@ -1,23 +0,0 @@ -# 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