Skip to content

Instantly share code, notes, and snippets.

@gijs
Forked from jamesarosen/04_jshint.rake
Created October 25, 2011 08:01
Show Gist options
  • Select an option

  • Save gijs/1311783 to your computer and use it in GitHub Desktop.

Select an option

Save gijs/1311783 to your computer and use it in GitHub Desktop.
Javascript Loves CI: Jenkins + Jasmine + PhantomJS + JSHint
namespace :jshint do
task :require do
sh "which jshint" do |ok, res|
fail 'Cannot find jshint on $PATH' unless ok
end
end
task :check => 'jshint:require' do
project_root = File.expand_path('../../', File.dirname(__FILE__))
config_file = File.join(project_root, 'config', 'jshint.json')
js_root_dir = File.join(project_root, 'public', 'console', 'javascripts')
files = Rake::FileList.new
files.include File.join(js_root_dir, '**', '*.js')
files.exclude File.join(js_root_dir, 'vendor', '**', '*.js')
sh "jshint #{files.join(' ')} --config #{config_file}" do |ok, res|
fail 'JSHint found errors.' unless ok
end
end
end
desc 'Run JSHint checks against Javascript source'
task :jshint => 'jshint:check'
# Remove the original definition of jasmine:ci, which relies on RSpec
# and Selenium.
Rake.application.instance_eval do
@tasks.delete 'jasmine:ci'
end
namespace :jasmine do
task :require_phantom_js do
sh "which phantomjs" do |ok, res|
fail 'Cannot find phantomjs on $PATH' unless ok
end
end
task :ci => ['jasmine:require', 'jasmine:require_phantom_js'] do
support_dir = File.expand_path('../../spec/javascripts/support', File.dirname(__FILE__))
config_overrides = File.join(support_dir, 'jasmine_config.rb')
require config_overrides if File.exists?(config_overrides)
phantom_js_runner = File.join(support_dir, 'phantom-js-runner.js')
config = Jasmine::Config.new
# start the Jasmine server and wait up to 10 seconds for it to be running:
config.start_jasmine_server
jasmine_url = "#{config.jasmine_host}:#{config.jasmine_server_port}"
puts "Running tests against #{jasmine_url}"
sh "phantomjs #{phantom_js_runner} #{jasmine_url}" do |ok, res|
fail 'Jasmine specs failed' unless ok
end
end
end
# in spec/javascripts/supports/jasmine_config.rb:
module Jasmine
class Config
# Add your overrides or custom config code here
attr_reader :jasmine_server_port
end
end
// in spec/javascripts/phantom-js-runner.js:
if (phantom.state.length === 0) {
if (phantom.args.length !== 1) {
console.log('Usage: run-jasmine.js URL');
phantom.exit();
} else {
phantom.state = 'run-jasmine';
phantom.open(phantom.args[0]);
}
} else {
window.setInterval(function () {
if (document.body.querySelector('.finished-at')) {
console.log(document.body.querySelector('.description').innerText);
var failed = document.body.querySelectorAll('div.jasmine_reporter .spec.failed .description');
for (var i = 0, desc; desc = failed[i]; i++) {
var message = [desc.title, desc.nextSibling.querySelector('.resultMessage.fail').innerText];
console.log(message.join(' => '));
}
phantom.exit(failed.length);
}
}, 100);
}
# Jenkins runs JSHint and Jasmine specs:
task :ci => [:jshint, 'jasmine:ci']
@tispratik
Copy link
Copy Markdown

Am getting an error on line 2 of phantom_js_runner.js saying that TypeError: 'undefined' is not an object
Do you know how to fix this?

Thanks!

→ phantomjs spec/javascripts/support/phantom-js-runner.js http://localhost:8888
TypeError: 'undefined' is not an object

@tispratik
Copy link
Copy Markdown

phantom.state is undefined. Btw, am running on rails2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment