# test/karma.conf.js
...
basePath: '<%= Bundler.root %>',
...
files: [
'<%= resolve_asset('vis.js') %>',
'app/assets/javascripts/app.js',
'test/**/*_spec.js'
],
# Rakefile
namespace :test do
task js: :environment do
with_tmp_karma_config do |config|
sh "./node_modules/karma/bin/karma start #{config} --single-run"
end
end
private
def with_tmp_karma_config
Tempfile.open('karma.js') do |f|
f.write ERB.new(File.read('test/karma.conf.js')).result(binding)
f.flush
yield f.path
end
end
def resolve_asset(file)
Rails.application.assets.find_asset(file).to_a.first.pathname.to_s
end
end
Tag Archives: Rails
Development checks for fast boottime
A few simple checks to make sure we are not regressing into slow boot times by preloading to many things. Needs to be the last initializer so it catches what all the others are doing -> zz.rb
Code
# config/initializers/zz.rb
if Rails.env.development?
# make sure we do not regress into slow startup time by preloading to much
Rails.configuration.after_initialize do
[
ActiveRecord::Base.send(:descendants).map(&:name),
ActionController::Base.descendants.map(&:name),
(File.basename($0) != "rake" && defined?(Rake) && "rake"),
].compact.flatten.each { |c| raise "#{c} should not be loaded" }
end
end
Result
config/initializers/zz.rb:9:in `block (2 levels) in ': User should not be loaded (RuntimeError)
Rails 3: Faster startup by not eager loading engines
Engine eager load by default on rails 3, so lets turn that off to save some precious startup time.
# config/environments/test.rb + development.rb config.eager_load = false
# config/application.rb
if !config.eager_load && Rails::VERSION::MAJOR == 3
Rails::Railtie.descendants.each do |railtie|
railtie.class_eval { def eager_load!; end }
end
end
Test startup time -33% by making fixtures not require
Small little patch, big test improvements 🙂
# do not load every class when using fixtures :all -> speedup ActiveRecord::TestFixtures::ClassMethods.class_eval do def require_fixture_classes(*args) end end
FYI merged Rails PR, will be in Rails 5, maybe 4.2.x 🙂
Stop rails from swallowing after_commit exceptions
Problem
By default rails just swallows any exception raised in after_commit blocks.
Solution
Send these exceptions to and exception service to get notified (Airbrake / Rollbar etc).
Gem
https://github.com/grosser/after_commit_exception_notification
Copy-paste
module Foo
module CommittedWithExceptions
def committed!
super
rescue Exception => e # same as active_record/connection_adapters/abstract/database_statements.rb:370
ExceptionService.report("after_commit exception", e)
raise
end
end
end
ActiveRecord::Base.include Foo::CommittedWithExceptions