JS Test Driver Rails
js-test-driver-rails is a thin wrapper for the JsTestDriver library: http://code.google.com/p/js-test-driver/
To take advantage of js-test-driver-rails, you should create a js_test_driver.rb file in your RAILS_ROOT/config/ directory.
The file may contain following directives:
Note, that this is a ruby file, so the file/browser list can be generated dynamically - it's completely up to you.
For example in our project we examine a list of known browsers to determine the ones installed on the developer's system, and define only those that were found.
Similarly we get the list of .js files to include from our asset packaging solution.
js-test-driver-rails also allows you to define HTML fixtures easily.
Imagine you have a directory structure like this:
RAILS_ROOT: test/ js/ fixtures/ foo/ a.html a.html
Then by defining the fixtures directory in your config file:
At runtime, your tests will have access to the contents of the fixture files:
htmlFixtures.all["a"] // contents of the test/js/fixtures/a.html htmlFixtures.all["foo/a"] // contents of the test/js/fixtures/foo/a.html
You can customize the namespace and fixture container name:
# the fixtures will be accessible through myApp.html["<fixture name goes here>"] fixtures "test/js/fixtures", :name => "html", :namespace => "myApp"
By default JsTestDriver comes with a pretty simple Test::Unit like testing framework. However it supports a couple of other frameworks, including Jasmine (http://pivotal.github.com/jasmine/), which is an RSpec-like BDD framework.
If you want to user Jasmine, simply add:
in your config file, somewhere before including your test files and you are golden:)
Measuring code coverage
JsTestDriver has a pretty cool feature of being able to measure code coverage. To enable this in your config, you just need to add:
in your config file.
You need to have lcov installed for the HTML report to be generated:
sudo apt-get install lcov
The HTML coverage report will be saved in the .js_test_driver/tests/coverage directory.
This gem comes with some rake tasks, which you should require in your Rake file:
To start the js test driver server:
To capture the default (or specified) browsers
rake js_test_driver:capture_browsers [BROWSERS=foo,bar,baz]
To run the tests (all or the specified ones)
rake js_test_driver:run_tests [TESTS=TestCase[.testMethod]]
To run the server, capture the browsers and run tests all in one command
rake js_test_driver:run [TESTS=TestCase[.testMethod]] [BROWSERS=foo,bar,baz] [OUTPUT_XML=1 | OUTPUT_PATH=/some/dir] [CAPTURE_CONSOLE=1]
This last task is mostly useful on CI, because it's much faster to run the server and capture the browsers once and then run the tests again and again in development mode.