Skip to content

Instantly share code, notes, and snippets.

@buruzaemon
Created April 24, 2012 00:22
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 buruzaemon/2474873 to your computer and use it in GitHub Desktop.
Save buruzaemon/2474873 to your computer and use it in GitHub Desktop.
phantomjs & jasminerice
group :test, :development do
gem 'jasminerice'
end
require 'open3'
# lib/tasks/js.rake
#
# Run with: % jruby -S rake js:test
#
namespace :js do
namespace :test do
task :start_jasmine_runner => :environment do
port = (ENV['JS_PORT'] || 5555)
Open3.popen3("jruby -S rails s -p #{port}")
sleep 30
puts "Rails + jasminerice started!"
puts
end
task :run_phantomjs => :environment do
port = (ENV['JS_PORT'] || 5555)
sh "bin\\phantomjs spec\\javascripts\\support\\phantomjs-runner http://localhost:#{port}/jasmine"
end
task :stop_jasmine_runner => :environment do
port = (ENV['JS_PORT'] || 5555)
sin, sout, serr = Open3.popen3("netstat -ano")
lines = sout.readlines
line = lines.grep(/:#{port}/).grep(/LISTENING/)
raise "Could not locate running test rails server!" if line.empty?
pid = line.first.split.last
Open3.popen3("taskkill /F /PID #{pid}")
puts
puts "Rails + jasminerice stopped."
end
end
desc "Run jasmine/coffeescript specs, headlessly"
task :test => ['js:test:start_jasmine_runner', 'js:test:run_phantomjs', 'js:test:stop_jasmine_runner']
end
/*
* spec/javascripts/support/phantom-runner
*
* Script for running coffeescript unit tests as made available
* via jasminerice.
*
* Usage: phantomjs phantom-runner [jasmine spec runner URL]
*/
var sys = require('system');
var page = require('webpage').create();
var url = sys.args[1] || 'http://localhost:5555/jasmine';
page.onConsoleMessage = function (msg) { console.log(msg); };
page.onError = function (msg, trace) {
console.log('ERROR: ', msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
page.onLoadStarted = function() {
console.log('Starting jasmine-based test runner...');
};
page.onLoadFinished = function(status) {
if (status !== 'success') {
console.log('ERROR: could not access jasmine spec runner at: ', url);
phantom.exit(-1);
} else {
var f = 0;
page.evaluate(function() {
if (document.body.querySelector('.finished-at')) {
console.log(document.body.querySelector('.finished-at').innerText);
console.log(document.body.querySelector('.description').innerText);
var failures = document.body.querySelectorAll('div.jasmine_reporter .spec.failed .description');
for (var i=0, desc; desc = failures[i]; i++) {
console.log("\n ", desc.title);
console.log(' ->', desc.nextSibling.querySelector('.resultMessage.fail').innerText);
}
f = failures.length;
}
});
phantom.exit(f);
}
};
page.open(url);
//= require application
//= require sinon
//= require_tree ./
/*
* HACK! HACK! HACK!
* This overrides corrects the generation of the path string
* to any fixtures you might use. Without this, the path to
* your fixtures will be different depending on whether the
* phantomjs browser is making the request or you are simply
* going thru a browser.
*/
jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(template_path) {
var data = JST[template_path]();
this.fixturesCache_[template_path] = data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment