Unit Testing JavaScript

I’ve stumbled across a really cool unit testing framework for JavaScript that I wanted to share with everyone. It is called js-test-driver. http://code.google.com/p/js-test-driver/ Most JavaScript unit test frameworks need to be manually run, which requires a dev/qe to open a browser, start the tests, and verify the results. This doesn’t really scale or work for a CI environment. Js-test-driver aims to solve that problem.

The software itself is built with java and has a client and server side component. To start the server you just pass in a port and fire it up. Ex: java -jar JsTestDriver-1.3.2.jar –port 4224 It also has the option of passing in a list of browser executables, which the server will automatically start and setup to listen for unit tests. Additional browsers can be provisioned manually across a variety of devices by pointing to http://{the_servers_IP}:4224 in any browser window. Think Mobile. The client side is run by setting up a config file with some general information, including the server to connect to and the JavaScript files to load.

Example: jsTestDriver.conf

server: http://localhost:4224
 
load:
  - bin/*.js
 
timeout: 90

The tests are then ran by calling the same jar file: java -jar JsTestDriver-1.3.2.jar –tests all

In my proof of concept, I was able to quickly get 8 browsers, (4 Windows browsers, 2 Mac browsers, iPad Safari, and iPhone Safari), up and running for testing against. The Unit Test itself was a very basic Hello World test. The command took more time initializing the jvm & connections than the actual unit tests, which only took 26ms to run across all of the browsers/devices.

Joshua@Quake ~/js-test-driver
$ java -jar JsTestDriver-1.3.2.jar --tests all
........
Total 8 tests (Passed: 8; Fails: 0; Errors: 0) (1.00 ms)
  Safari 533.21.1 Windows: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)
  Safari 6533.18.5 iPhone OS: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (1.00 ms)
  Firefox 4.0.1 Windows: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (1.00 ms)
  Firefox 4.0.1 Mac OS: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)
  Safari 6533.18.5 Mac OS: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (1.00 ms)
  Chrome 12.0.742.100 Windows: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)
  Safari 533.21.1 Mac OS: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (1.00 ms)
  Microsoft Internet Explorer 9.0 Windows: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)
 
Joshua@Quake ~/js-test-driver
$