Skip to content

Instantly share code, notes, and snippets.

@amkisko
Created December 2, 2025 09:34
Show Gist options
  • Select an option

  • Save amkisko/6958a975050b2c59e55f415131a8236a to your computer and use it in GitHub Desktop.

Select an option

Save amkisko/6958a975050b2c59e55f415131a8236a to your computer and use it in GitHub Desktop.
ruby playwright rspec for custom node version provisioning
module TestEnv
ASSETS_DIGEST_FILE = Rails.root.join("tmp/test_assets_digest.txt")
PLAYWRIGHT_VERSION_FILE = Rails.root.join("tmp/playwright_version.txt")
PLAYWRIGHT_COMMAND = "NODENV_VERSION=25.2.1 NODE_VERSION=25.2.1 npx -y playwright"
class ::Pathname
def digest
if directory?
`find "#{self}" -type f -print0 | sort -z | xargs -r0 md5sum | md5sum | awk '{print $1}'`.strip
end
end
end
def self.test_assets_digest
app_digest = Rails.root.join("app").digest
public_digest = Rails.public_path.digest
[app_digest, public_digest].join(" ")
end
def self.test_assets_precompiled?
ENV["SKIP_ASSETS_PRECOMPILE"].present? || !ENV["PRECOMPILE_ASSETS"] && ASSETS_DIGEST_FILE.exist? && ASSETS_DIGEST_FILE.read == TestEnv.test_assets_digest
end
def self.test_assets_precompile
return if TestEnv.test_assets_precompiled?
puts "Precompiling assets for system tests..."
system("bin/rails assets:precompile" + (ENV["DEBUG"].blank? ? " >/dev/null" : ""))
raise "Failed to precompile assets" unless $CHILD_STATUS.success?
ASSETS_DIGEST_FILE.write(TestEnv.test_assets_digest)
end
def self.test_assets_clobber
return unless TestEnv.test_assets_precompiled?
puts "Clobbering assets for system tests..."
system("bin/rails assets:clobber" + (ENV["DEBUG"].blank? ? " >/dev/null" : ""))
raise "Failed to clobber assets" unless $CHILD_STATUS.success?
ASSETS_DIGEST_FILE.delete
end
def self.playwright_version
`#{PLAYWRIGHT_COMMAND} --version`.strip
end
def self.playwright_installed?
PLAYWRIGHT_VERSION_FILE.exist? && PLAYWRIGHT_VERSION_FILE.read.strip == TestEnv.playwright_version
end
def self.playwright_install
return if TestEnv.playwright_installed?
puts "Installing Playwright..."
system("#{PLAYWRIGHT_COMMAND} install --force" + (ENV["DEBUG"].blank? ? " >/dev/null" : ""))
raise "Failed to install Playwright" unless $CHILD_STATUS.success?
PLAYWRIGHT_VERSION_FILE.write(TestEnv.playwright_version)
end
def self.before_suite
unless $system_ready # standard:disable Style/GlobalVars
if `lsof -i :3035`.present?
puts "Stopping webpack-dev-server..."
system("kill -9 $(lsof -i :3035 -t)")
end
TestEnv.test_assets_precompile
TestEnv.playwright_install
$system_ready = true # standard:disable Style/GlobalVars
end
end
def self.after_suite
# TestEnv.test_assets_clobber if $system_ready # standard:disable Style/GlobalVars
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment