I've been experimenting with a new tool that was released open source recently called JsTestDriver
It's a tool that makes unit testing JavaScript simultaneously on many browsers from the command line very easy for the developer. Actually, there aren't many other tools like it that I know of. It also provides hooks for continuous integration (e.g. Xunit output) and is designed to help you run all unit tests when you click the save button in your code editor.
Here are some features it provides that I thought were nice:
- The browser environment is persistent. You don't deal with URLs so functional / system testing is not possible, just unit testing. This is by design because it is thus very fast to run tests; the runner only has to reload the scripts that have changed by modified timestamp. Instead of URLs and html, you just edit one config file in YAML to say which scripts to load and you write all tests in JavaScript.
- A simple JUnit class in JavaScript is provided. It has setUp / tearDown, assertEquals, everything you'd expect
- You run your tests from a java jar on the command line. It's fast and easy.
- Connecting multiple browsers is very straight forward. You just open a magic URL like http://127.0.0.1:4224/capture/ and the browser becomes a worker to your command line test runner. This makes farming out your browsers to multiple servers on other operating systems very easy.
- Tracebacks, whee! Errors and command line output is decent.
- jquery 1.3.2 is provided. You have to be careful to not include your own version though since reloading jquery can cause
$(document).ready(fn)
to stop working and probably other cache-related errors. - You can select tests to run on the command line by their JavaScript object name.
- The DOM is actually reset before each test which is convenient. You can add elements in your setUp() if you need to like
$("body").add("div").attr("id","foo")
. See Manipulating The DOM for more info. - You can use
assertExpects(n)
to help make sure that assertions in callbacks are actually made. I was never a huge fan of counting asserts in tests but this works pretty well as a sanity measure.