Dr Nic

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”.

Related posts:

  1. Instant new Rails applications with the App Scrolls When I start a new project I want to start...
  2. Using CoffeeScript in Rails and even on Heroku I’m pretty excited about CoffeeScript as a clean-syntax replacement for...
  3. First look at rails 3.0.pre This article is out of date in some aspects....
  4. Rails themes can remember things I was getting annoyed at having to remember all the...
  5. Install any HTML theme/template into your Rails app Have you ever even bothered to Google for “rails...

14 Responses to “One-stop JavaScript Unit Testing for Rails 2.0”

  1. Subbu says:

    Interesting. Are you going to do this peepcode?

  2. Dr Nic says:

    @Subbu [via] – yep; it has driven my desire to make it easy to do JS testing. It seems better to write more code and talk less on the peepcode than the other way around :)

  3. Subbu says:

    I still have some credits left at peepcode. I’ll wait for your episode :)

  4. will bailey says:

    Hey Dr Nic,

    Can this gem be used in a rails 1.2.6 project? We’ve not yet upgraded to Rails 2, but I’d like to take advantage of this cool gem you’ve developed.

    Thanks!

  5. Dr Nic says:

    @will bailey [via] – the mechanism that makes the generator available to rails is new to rails 2.0 – it can find generators within gems (within /rails_generator folders).

    What you can do is create a ~/.rails/generators folder, and copy the entire generator folder from within the gem into it. Then all your rails applications will have access to the generator. Make sense?

  6. Miho says:

    Hi Dr Nic,

    Thank you very much for your javascript_unittest plugin. We are enjoying it very much:)

    One question though. Seems like when we run tests from rake command, we don’t get the feedback of failures and errors. Here is the output from rake command.

    $ rake test:javascripts

    Started tests in Firefox
    ..
    Finished in 1 seconds.
    0 tests, 104 assertions, 0 failures, 0 errors

    We have failures and errors and see them red on the browser. I’m wondering if there is a way for us to see that in the terminal rather than checking the browser every time. And if there is a way, it will help us to make it automated test!

  7. Dr Nic says:

    Hmm, it should display errors back in the terminal – that’s kind of its purpose. I don’t use it much so I haven’t noticed it behaving badly. The code comes from the prototypejs project – so they might have some more recent commits to fix any issues etc, and these fixes can be ported back across.

  8. Miho says:

    Hi Dr Nic, thank you for your reply.
    Turned out if you have more than 1 Test.Unit.Runner within 1 xxx_test.html file, you will not get the results (number of failures and errors) back to the terminal. Now we have separated all the Test.Unit.Runner to each xxx_test.html file, it’s all good. We should have done this way from the beginning anyways!

  9. Dr Nic says:

    You can have 2xRunners. There should be an inline comment decribing what to do.

  10. James Seigel says:

    Just wondering if you’ve managed to integrate this into something like cruisecontrol.rb in a way that would close the browsers after?

    Cheers
    James.

  11. [...] Multiruby looks like it may do what I want. I already have it installed, because I am autotest-addicted (first coined by Dr Nic. [...]

  12. Social comments and analytics for this post…

    This post was mentioned on Twitter by bjhess: What’s everyone doing for JS Unit testing? Thought javascript_test plugin, then saw this: http://bit.ly/1HiIXg. Not sure where to go……