It's actually fairly simple: I'm using the capybara selenium driver to run the JSpec tests and grab the results from the DOM when they finish. Here's the testcase:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/../test_helper' | |
class JSpecTest < ActionController::IntegrationTest | |
include Capybara | |
def setup | |
@wait_time = Capybara.default_wait_time | |
Capybara.default_wait_time = 15 | |
end | |
['outright.ledgerTest.js', | |
'outright.txnRendererTest.js', | |
'outright.txnTest.js', | |
'outright.txnRowTest.js', | |
'outright.userPreferenceTest.js', | |
'outright.mileageRateTest.js', | |
'outright.categoryFieldTest.js'].each do |jspec| | |
define_method "test_#{jspec.gsub(".", "_").gsub(/_js$/, "").underscore}" do | |
run_jspec(jspec) | |
end | |
end | |
def run_jspec(jspec_file) | |
# Demetertacular!! | |
page.driver.browser.navigate.to "file://#{Rails.root}/test/javascripts/spec.browser.html?test=#{jspec_file}" | |
assert page.has_css?(".failures") | |
assert_match /\s+0$/, page.locate(:css, ".failures").text, "#{jspec_file} failed." | |
# puts page.all(:css, ".fail").each {|failure| failure.text } | |
end | |
def teardown | |
Capybara.default_wait_time = @wait_time | |
end | |
end |
From here, running it under CI is trivial. In fact I didn't have to do anything at all, the tests was found by rake test:integrations and ran (and passed!) in the next build. Most of the heavy lifting is done by capybara and selenium-webdriver.
Obviously there is a lot that could be done to improve this code. I could grab the jspec file from a FileList, and grab some better failure messages from the DOM. I could also probably write a custom JSpec formatter to make this easier on myself even. I'm thinking if there is interest I could finish this up and turn it into a gem. Please comment if you'd use such a thing.
Also, one more thing you will need is the JSpec runner file that can run a single test. I did that like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
function runSuites() { | |
// uncomment when we move away from the grammar | |
//JSpec.preprocess = function(code) { return code; } | |
var matches = /.*\?test=(.*)/.exec(document.URL); | |
if (matches) | |
{ | |
JSpec | |
.exec(matches[1]) | |
.run({ failuresOnly: false, fixturePath: 'fixtures' }) | |
.report(); | |
} else { | |
// run the whole suite | |
} | |
} | |
</script> |
Enjoy!