Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Created January 12, 2015 02:41
Show Gist options
  • Select an option

  • Save BenMorganIO/f0d812cdf18a970d7851 to your computer and use it in GitHub Desktop.

Select an option

Save BenMorganIO/f0d812cdf18a970d7851 to your computer and use it in GitHub Desktop.
Karma with Rails
namespace :karma do
task start: :environment do
with_tmp_config :start
end
task run: :environment do
with_tmp_config :start, "--single-run"
end
private
def with_tmp_config(command, args = nil)
Tempfile.open("karma_unit.js", Rails.root.join("tmp")) do |f|
f.write unit_js(application_spec_files)
f.flush
system "./node_modules/.bin/karma #{command} #{f.path} #{args}"
end
end
def application_spec_files
assets = Rails.application.assets
assets.append_path Rails.root.join("spec/javascripts/support/karma")
assets.find_asset("application_spec.js").to_a.map { |e| e.pathname.to_s }
end
def unit_js(files)
unit_js = File.open("spec/javascripts/support/karma/config/unit.js", "r").read
unit_js.gsub "APPLICATION_SPEC", "\"#{files.join("\",\n\"")}\""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment