Skip to content

Instantly share code, notes, and snippets.

@daino3
Last active November 3, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daino3/a39486ff8bfc1668e923 to your computer and use it in GitHub Desktop.
Save daino3/a39486ff8bfc1668e923 to your computer and use it in GitHub Desktop.
require 'pry'
namespace :karma do
task :start => :environment do
with_tmp_config :start
end
task :run => :environment do
exit with_tmp_config :start, "--single-run"
end
private
def with_tmp_config(command, args = nil)
# by default karma looks for a file name "karma.conf.js" so let's not reinvent the wheel here
File.open(Rails.root.join('karma.conf.js'), 'w') do |f|
f.write unit_js(application_spec_files)
f.flush
# karma.conf.js should be in root directory that the command is run in.
# supplying the file path may work locally, but was giving me issues on
# a remote builder
system "./node_modules/karma/bin/karma #{command} #{args}"
end
end
def application_spec_files
Rails.application.assets.each_file.select { |pathname| pathname.match(/spec\/javascripts\/app/) }
end
def unit_js(files)
unit_js = File.open('spec/javascripts/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