Created
September 7, 2020 04:43
-
-
Save philiplambok/e8364d211d781c47cc067f0b520c1f54 to your computer and use it in GitHub Desktop.
webpacker.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prolog: | |
# | |
# In default rails testing setup we actually will running assets compiling in every thread | |
# In this case, we doing 8 times compiling assets (becuase we use parallel_test for 8 threads). | |
# And in some cases (thread) our compiling doesn't work as expected, so, this module will help us | |
# to only run assets compiling at once. | |
# | |
# This code was taken from: | |
# https://makandracards.com/makandra/46247-how-to-make-webpacker-compile-once-for-parallel-tests-and-only-if-necessary | |
# | |
# rubocop:disable Style/AccessModifierDeclarations, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | |
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| File.read(filename) if File.file?(filename) } | |
.join | |
digest = Digest::SHA256.hexdigest(packable_contents) | |
return if digest_file.exist? && digest_file.read == 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_file.read == digest | |
sleep 0.1 | |
end | |
end | |
end | |
end | |
# rubocop:enable Style/AccessModifierDeclarations, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | |
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/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment