Posted about 3 years ago. Visible to the public.

How to make Webpacker compile once for parallel tests, and only if necessary

Webpack is the future. We're using it in our latest Rails applications.

For tests, we want to compile assets like for production.
For parallel tests, we want to avoid 8 workers compiling the same files at the same time.
When assets did not change, we do not want to spend time compiling them.

Here is our solution for all that.

Its concept should work for all test suites. For Cucumber integration tests, place the following into features/support/webpacker.rb.

module WebpackerTestSupport module_function def compile_once digest_file = Rails.root.join("tmp/webpacker_#{Rails.env}_digest") app_code = Dir[Webpacker.config.source_path.join('**/*')] npm_code = Dir[Rails.root.join('yarn.lock')] packable_contents = (app_code + npm_code) .sort .map { |filename| if File.file?(filename) } .join digest = Digest::SHA256.hexdigest(packable_contents) return if digest_file.exist? && == digest if ENV['TEST_ENV_NUMBER'].to_i < 1 public_output_path = Webpacker.config.public_output_path FileUtils.rm_r(public_output_path) if File.exist?(public_output_path) puts "Removed Webpack output directory #{public_output_path}" Webpacker.compile digest_file.write(digest) else loop do break if digest_file.exist? && == digest sleep 0.1 end end end end WebpackerTestSupport.compile_once # The configured asset host will become a `<base>` tag in the HTML's `<head>` and allow resolving test assets via the # development Rails server. This makes HTML-only screenshots for non-`@javascript` Scenarios prettier. # Note that you won't be using development assets, but the compiled files from inside `RAILS_ROOT/public/packs-test/`. Capybara.asset_host = 'http://localhost:3000/'

That's it. Enjoy! 🎉

Similar scenario: running an application with Webpacker on multiple servers. How to serve identical assets from all servers.

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Arne Hartherz
Last edit:
25 days ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more