Skip to content

Commit

Permalink
rake test runs tests with PhantomJS
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 9, 2012
1 parent 3f6172d commit 5f221a2
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'guard/guard'

class ::Guard::PhantomJS < ::Guard::Guard
def run_all
run_on_change([])
end

def run_on_change(paths)
passed = system @options[:runner], *paths

::Guard::Notifier.notify \
(passed ? 'passed' : 'failed'),
:title => 'Zepto test results',
:image => (passed ? :success : :failed)
end
end

guard :phantomjs, :runner => 'script/test' do
watch(%r{src/(.+)\.js$}) do |m|
"test/#{m[1]}.html"
end
watch(%r{test/(.+)\.html$}) do |m|
m[0] unless m[1].include? '_functional'
end
end
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ $ gem install uglifier
Build Zepto by running `rake`:

~~~ sh
# build zepto.js
$ rake
Original version: 40.423k
Minified: 18.776k
Expand Down Expand Up @@ -174,7 +173,22 @@ browser to run the tests. Files named with "\_functional" are not automated
tests, but require interaction. Automated tests are written using
[Evidence.js][].

Detailed test output is logged in the JavaScript console of your browser.
Detailed test information such as specific failures is logged to the JavaScript
console of your browser.

It's possible to run automated tests from the command-line in headless Webkit
with [PhantomJS][]:

~~~ sh
# install on Mac OS
$ brew install phantomjs

# run all tests
$ rake test

# run individual files
$ script/test test/selector.html
~~~

### Code style guidelines

Expand All @@ -194,3 +208,4 @@ Detailed test output is logged in the JavaScript console of your browser.
[mkd]: http://github.github.com/github-flavored-markdown/
[evidence.js]: https://github.com/tobie/Evidence
[optional]: http://mislav.uniqpath.com/2010/05/semicolons/
[phantomjs]: http://code.google.com/p/phantomjs/wiki/Installation
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ end

task(:clean) { rm_rf 'dist' }

desc "Run tests with PhantomJS"
task :test do
sh 'script/test'
end

desc "Strip trailing whitespace and ensure each file ends with a newline"
task :whitespace do
verbose false do
Expand Down
6 changes: 6 additions & 0 deletions script/guard
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
if [ -z "$BUNDLE_GEMFILE" ]; then
export BUNDLE_GEMFILE=shutup
fi

exec guard --no-notify "$@"
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
phantomjs test/runner.coffee "$@"
8 changes: 5 additions & 3 deletions test/evidence_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@
var container = $('results')
if (container) {
if (results.failureCount || results.errorCount) {
container.innerHTML = printf("Finished in %d s. &ndash; <em>%d failures, %d errors</em> (%d assertions)",
[seconds, results.failureCount, results.errorCount, results.assertionCount])
container.className = 'failed'
container.innerHTML = printf("Finished in %d s. &ndash; <em>%d failures, %d errors</em> (%d assertions)",
[seconds, results.failureCount, results.errorCount, results.assertionCount])
} else {
container.innerHTML = printf("Finished in %d s. &ndash; <em>%d tests passed</em> (%d assertions)", [seconds, results.testCount, results.assertionCount])
container.className = 'passed'
container.innerHTML = printf("Finished in %d s. &ndash; <em>%d tests passed</em> (%d assertions)",
[seconds, results.testCount, results.assertionCount])
}
container.className += ' finished'
}
}

Expand Down
69 changes: 69 additions & 0 deletions test/runner.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Test runner for PhantomJS <phantomjs.org>
# Usage:
# $ phantomjs test/runner.coffee [<page1>, <page2>, ...]
#
# When no test pages specified, runs all automated tests.

system = require('system')
fs = require('fs')

if system.args.length > 1
# list of test pages to run
suites = system.args.slice(1)
else
# by default, run all test/*.html pages
modules = 'zepto ajax data detect event form fx selector stack'.split /\s+/
suites = modules.map (name)-> "test/#{name}.html"

page = require('webpage').create()

page.onConsoleMessage = (msg) ->
console.log msg

page.onError = (msg, trace) ->
console.log 'ERROR: ' + msg

# used for waiting until the tests finish running
waitFor = (testFn, onReady, timeout=3000) ->
start = new Date()
interval = setInterval ->
if testFn()
clearInterval interval
onReady()
else if new Date() - start > timeout
console.log "timed out."
phantom.exit(1)
, 100

loadNextSuite = ->
if not suites.length
phantom.exit()
else
url = suites.shift() + "?verbosity=WARN"
# PhantomJS chokes on the query string on relative paths
url = "file://#{fs.workingDirectory}/#{url}" if not /:\/\//.test url

page.open url, (status) ->
if status isnt "success"
console.log "failed opening #{url}"
phantom.exit(1)

waitFor ->
page.evaluate ->
# the "#results" element needs to have the "finished" class
res = document.getElementById 'results'
/finished/.test res.className if res
, ->
passed = page.evaluate ->
res = document.getElementById 'results'
paths = location.pathname.split('/')
# echo test results to the console
console.log "#{paths[paths.length - 1]} - " + res.textContent
/passed/.test res.className

if passed
loadNextSuite()
else
phantom.exit(1)

loadNextSuite()

0 comments on commit 5f221a2

Please sign in to comment.