Skip to content

Commit

Permalink
Change rake test:js to run headless.
Browse files Browse the repository at this point in the history
To run tests in browser, use rake test:js_in_browser.

Signed-off-by: Anurag Priyam <anurag08priyam@gmail.com>
  • Loading branch information
yeban committed Dec 26, 2014
1 parent d75a8ba commit c7df9de
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem 'puma', '~> 2.8.1'
gem 'sinatra', '~> 1.4.4'

gem 'minitest', '~> 5.3.1'
gem 'capybara', '~> 2.4.4'
gem 'capybara-webkit', '~> 1.3.1'
10 changes: 8 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ task 'test:pl' do
system 'prove -I tests/pl/lib -I . -lr tests'
end

desc 'Run unit tests for JS code.'
task 'test:js' do
desc 'Run unit tests for JS code in browser.'
task 'test:js_in_browser' do
require_relative 'app'
static = Class.new(Sinatra::Base)
static.public_dir = Dir.pwd
Expand All @@ -163,4 +163,10 @@ task 'test:js' do
App.init_server.serve(static)
end

desc 'Run unit tests for JS code headless, using Capybara.'
task "test:js" do |t|
require_relative 'tests/js/runner'
CapybaraJasmine.new.run
end

task default: [:deps, :'db:init', :'db:migrate', :configure]
30 changes: 30 additions & 0 deletions tests/js/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ require(['jasmine/jasmine']
timer: new jasmine.Timer()
});

/**
* Extend jsApiReporter for console output via Capybara.
*/
jasmineInterface.jsApiReporter.consoleOutput = function () {
var errors = [];
var output = [];
this.passed = true;
for (var i in this.specs()) {
var spec = this.specs()[i];
var name = spec.fullName;
if (spec.status === 'failed') {
for (var j in spec.failedExpectations) {
var item = spec.failedExpectations[j];
if (!item.passed) {
this.passed = false;
errors.push("Failed: " + name + "\n" + item.message);
output.push("F");
}
}
} else {
output.push(".");
}
}
output = output.join("")
if (errors.length > 0) {
output += "\n\n" + errors.join("\n\n") + "\n";
}
return output;
};

/**
* The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript.
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/js/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'sinatra'
require 'capybara-webkit'

# Run Jasmine specs headless using Capybara.
class CapybaraJasmine

include Capybara::DSL

def initialize
Capybara.current_driver = :webkit
Capybara.app = app
end

def run
visit '/tests/js/index.html'
sleep 0.05 until evaluate_script("window.jsApiReporter &&
window.jsApiReporter.finished")
puts evaluate_script "window.jsApiReporter.consoleOutput()"
exit evaluate_script "window.jsApiReporter.passed"
end

def app
static = Class.new(Sinatra::Base)
static.public_dir = Dir.pwd
static
end
end

0 comments on commit c7df9de

Please sign in to comment.