Skip to content

Instantly share code, notes, and snippets.

@rgarner
Last active July 21, 2022 13:20
Show Gist options
  • Save rgarner/01e56068b3c665aaca2f4bc7a53f0f39 to your computer and use it in GitHub Desktop.
Save rgarner/01e56068b3c665aaca2f4bc7a53f0f39 to your computer and use it in GitHub Desktop.
A rake-based approach to integrate jsbundling-rails into RSpec runs that only rebuilds when JS has changed.
# lib/tasks/asset.rake
#
# Hook up to jsbundling-rails by making spec:prepare dependent
# on app/assets/builds/application.js, a file-based equivalent to
# javascript:build. Will only rebuild when JS files have changed
if Rails.env.test?
ASSET_SOURCE_DIR = 'app/javascript'.freeze
ASSET_BUILDS_DIR = 'app/assets/builds'.freeze
JS_SOURCE = FileList["#{ASSET_SOURCE_DIR}/**/*"]
#
# application.js is symbolic for "all the app JS and sourcemaps, timestamped".
# It will only rebuild if any file found in the JS_SOURCE FileList is newer.
file "#{ASSET_BUILDS_DIR}/application.js" => JS_SOURCE do
unless system 'yarn install && yarn build'
raise 'assets.rake: Command build failed, ensure yarn is installed and `yarn build` runs without errors'
end
end
Rake::Task['spec:prepare'].enhance(["#{ASSET_BUILDS_DIR}/application.js"])
end
# /spec/rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
Rails.application.load_tasks
Rake::Task['spec:prepare'].invoke
Dir[Rails.root.join('spec', 'support', 'config', '*.rb')].each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
require 'byebug/core'
Byebug.mode = :off if ENV['DISABLE_BYEBUG']
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include ActiveJob::TestHelper, type: :feature
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment