One-stop JavaScript Unit Testing for Rails 2.0
Previously, I mentioned a multi-step/multi-project solution to doing JavaScript Unit Testing for Rails, including an autotest script to make TDD life easier for the autotest-addicted.
It was too many steps, too many different projects, and too much dependence on prototypejs. So let’s fix this via the spanky new newjs (introduced here):
Instead of several steps, there is now two steps:
cd myrailsapp # rails 2.0 app
sudo gem install newjs # v1.3.1+
script/generate javascript_test application
create vendor/plugins/javascript_unittest/lib
create vendor/plugins/javascript_unittest/tasks
create test/javascript/assets/jsunittest.js
create test/javascript/assets/unittest.css
create config/javascript_test_autotest.yml.sample
create vendor/plugins/javascript_unittest/lib/jstest.rb
create vendor/plugins/javascript_unittest/tasks/runner.rake
create vendor/plugins/javascript_unittest/tasks/autotest.rake
create vendor/plugins/javascript_unittest/README
create test/javascript/application_test.html
create script/rstakeout
create script/js_autotest
It installs all assets, gives you a rake test:javascripts task, gives you script/js_autotest, and finally (and most importantly), creates the application_test.html test stub.
As always, you can also run a single test file by loading it into a browser.
Prototype independence
Previously, the test suite - unittest.js - had a dependency on prototypejs. This was wonderful if you’re developing in prototypejs, but could cause some grief if you weren’t.
newjs now comes packed with an identical test suite - jsunittest - but it is built without a dependency on prototypejs.
Merb et al support
I wrote a rails generator because I used Rails. If a merb/other framework (Ruby or non-Ruby) wants a similar generator, just ask. Its pretty simple - I copy over the rails_generators folder into a merb_generators folder in newjs and change some things based on your frameworks’ structure.
Alternate test suite support
If you like another test suite, and want it included in the generators, this might require some additional effort on your part. It probably has different mechanisms for launching runners etc; whereas I was able to steal the rake tests + support libraries from prototypejs. But, it can be done.
Peepcode
These tools will be discussed in animated detail in the forth coming Peepcode “JavaScript Unit Testing”.
