I’m currently in the middle of writing a reusable little JavaScript utility, which I’ll probably release when it’s finished, for one of my many projects and I wanted to employ unit testing as part of the development process, as I believe in the benefits of it quite strongly.

However my previous experience with unit testing JavaScript haven’t been the most pleasent, I have tried the most common nUnit variations for JS, the best of which was JSUnit. But I always found this a little cumbersome and difficult to use, however I had recently read about the unit testing available with script.aculo.us and although the only documentation I could find was the PDF available at mir.aculo.us I thought I’d give it a go.

My only worry with using the script.aculo.us unit testing was its dependency on prototype and script.aculo.us (obviously) and now I’ve moved across to mootools for most things I thought there may be some clashes between prototype and mootools for this.

However with the current versions of each of those I managed to get everything working by changing only two lines in the unittest.js:

Event.observe(td, 'click', function() ...

to

$( td ).addEvent( 'click', function() ...

The script.aculo.us unit testing framework is very fast (one of my tests performs 16 tests with 1904 assertions in about a second) and it does a great job of catching scripting and syntax errors and presenting those in a useful way without breaking the tests, which some frameworks I’ve used in different languages don’t always handle too well.

The only let down is the current level of documentation, but if you’ve looked at unit testing in any other language this is almost the same and you can figure out what most of the assertions do from reading the PDF and looking at the source. The only minor niggle is the need to use the

with( this ) {
	...
}

syntax for all your assertions, I haven’t found an explanation for why this is needed within each of the test methods and why it can’t be hidden from the user, but you need it otherwise it won’t work.

I haven’t looked at using the functional tests available within script.aculo.us yet as it’s still at a very early stage and I’m not sure whether it’s needed as I think I can do everything I need right now with unit tests. I hope to write more about specific examples of unit tests, e.g. complex ones which require DOM manipulation or user interaction etc., in JavaScript when I have some decent examples in place.