Skip to content

Instantly share code, notes, and snippets.

@turhn
Created December 8, 2014 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turhn/537495bdb3fa5c9de230 to your computer and use it in GitHub Desktop.
Save turhn/537495bdb3fa5c9de230 to your computer and use it in GitHub Desktop.
Configuring a Rake Task for karma and jasmine
require 'open3'
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
puts "Running karma with configuration file: #{f.path}"
puts 'Open Chrome -> localhost:9676/debug.html'
Open3.popen2e("karma start #{f.path} #{args}") do |stdin, stdout_and_stderr, wait_thr|
puts stdout_and_stderr.gets(nil)
raise "Karma tests failed" unless wait_thr.value.success?
end
end
end
def application_spec_files
Rails.application.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/karma.unit.js', 'r').read
unit_js.gsub "APPLICATION_SPEC", "\"#{files.join("\",\n\"")}\""
end
end
// Karma configuration
// Generated on Thu Nov 13 2014 18:24:05 GMT+0200 (EET)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '..',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
APPLICATION_SPEC,
'spec/javascripts/**/*_spec.coffee'
],
// list of files to exclude
exclude: [
'**/*.erb'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.coffee': ['coffee']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment