-
-
Save TaniaSwan/01a3ba1d57a80bef9635dec10cbadb75 to your computer and use it in GitHub Desktop.
Run webpacker before test suite, only if a test tagged with JS is selected
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
# spec/support/webpack.rb | |
module WebpackTestBuild | |
TS_FILE = Rails.root.join("tmp", "webpack-spec-timestamp") | |
class << self | |
attr_accessor :already_built | |
end | |
def self.run_webpack | |
puts "running webpack-test" | |
`RAILS_ENV=test bin/webpack` | |
self.already_built = true | |
File.open(TS_FILE, "w") { |f| f.write(Time.now.utc.to_i) } | |
end | |
def self.run_webpack_if_necessary | |
return if self.already_built | |
if timestamp_outdated? | |
run_webpack | |
end | |
end | |
def self.timestamp_outdated? | |
return true if !File.exists?(TS_FILE) | |
current = current_bundle_timestamp(TS_FILE) | |
return true if !current | |
expected = Dir[Rails.root.join("app", "javascript", "**", "*")].map do |f| | |
File.mtime(f).utc.to_i | |
end.max | |
return current < expected | |
end | |
def self.current_bundle_timestamp(file) | |
return File.read(file).to_i | |
rescue StandardError | |
nil | |
end | |
end | |
RSpec.configure do |config| | |
config.before(:each, :js) do | |
WebpackTestBuild.run_webpack_if_necessary | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment