Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run webpacker before test suite, only if a test tagged with JS is selected
# 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
@TaniaSwan

This comment has been minimized.

Copy link

TaniaSwan commented Dec 29, 2017

Worked for me.

@bbugh

This comment has been minimized.

Copy link

bbugh commented Oct 1, 2018

Thanks for this! It works great.

Using the Rails built-in command Webpacker.compile in place of the backtick shell command may behave better. It returns a boolean based on success, and you can add better error handling to it.

puts "running webpack-test"
unless Webpacker.compile
  raise "Error: webpacker failed to build. Run 'bin/webpack', fix any issues, and try again."
end
self.already_built = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.