-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures_helper.rb
78 lines (60 loc) · 2.07 KB
/
features_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'rails_helper'
require 'capybara/rails'
# stub logged in devise user w/ capybara
# @see https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
include Warden::Test::Helpers
Warden.test_mode!
# Webdrivers::Chromedriver.required_version = "118.0.5993.70"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[no-sandbox headless disable-gpu])
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
# set to :chrome to see real browser open up
# Capybara.default_driver = :chrome
# Capybara.javascript_driver = :chrome
Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
Capybara.server = :webrick
# webpacker not automatically compiling before tests run, workaround from:
# https://github.com/rails/webpacker/issues/59#issuecomment-295273400
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"
puts `which node`
puts `node --version`
`RAILS_ENV=test bin/shakapacker`
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 ENV['CIRCLECI'] # webpack is explicitly compiled in the build step of circleci
return if self.already_built
run_webpack if timestamp_outdated?
end
def self.timestamp_outdated?
return true if !File.exist?(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