Last week a colleague asked for my help finding better unit test tools for Java Script. He’s done some digging on the state of the art with JavaScript unit tests and finds the whole lot wanting. His discoveries:
| Tool | Pros | Cons |
| Jsunit: we already use it for some of our js code. |
|
|
| rhinounit |
|
|
| crosscheck: Note: Crosscheck wasn’t tested with any code. |
|
|
| jsspec |
|
|
| jspec (no website – just a source tree) |
|
|
| Screw.unit (docs) Note: Not tested but it is very similar to jsspec and jspec. |
|
|
So it seems to him that Jsunit is the only choice we have. It is not perfect though because it does not provide an easy way to apply the TDD process for the following reasons:
- It does not provide a simple and integrated way to run JavaScript unit test
- It forces you to write the unit tests in a html file instead of a .js file.
- It forces you to have a local installation of the jsunit framework in order to avoid absolute hard coded path to reference js unit files.
As a consequence, you have to switch back and forth from you IDE and all the browsers we want to support while "TDDing" in JavaScript. It is feasible but doesn’t seem very effective.
I tried asking about this on StackOverflow generated some interesting answers:
- Look at the Dojo Object Harness (DOH) unit test framework which is pretty much framework independent harness for JavaScript unit testing and doesn’t have any Dojo dependencies. I found a very good description of it at Medrix Observations: Dojo D.O.H. Unit Testing. Interestingly we have at least one team (Yvon’s) that is experimenting with Dojo. (got several recommendations for this)
- Apparently MochiKit – yet another library has a framework called SimpleTest buried inside it.
- Yahoo Unit – a standalone tool from Yahoo.
Asking on the Test Driven Mailing List got another batch of answers:
- QUnit from jQuery (got several recommendations for this)
- Mocking tool for JavaScript
- Use GWT and do all your work in Java
In addition two people didn’t answer the question directly but instead sent my in the direction of some books:
- Secrets of the JavaScript Ninja by the project lead for jQuery (which will have a chapter on Unit Testing)
Conclusions
There isn’t one good place to ask JavaScript/Unit Testing questions. The best so far seems StackOverflow.com seems to be the only real option.
Of the Unit Test Frameworks the real options seem to be:
- JSunit
- The Dojo Object Harness (DOH) unit test framework
- QUnit from jQuery
- YUI Test: see YUI Test – The New Kid on Block
I also did some digging for Mock Frameworks and have only come up with a list of tools:
- JSMock – JSMock provides expectation recording and matching, and has the ability to return, throw, and stub on object method calls
- Jack – The project aims to help developers write short and readable JavaScript tests.
- MockMe – written because of Johanne’s dissatisfaction with other JavaScript Mock tools.
- QMock – very new
There is some good writing that will give you a flavour of TDD with Javascript:
- Test Driven AJAX a presentation from Agile 2008 (long but through)
- TDD and Javascript with JsMock
- Test Driven Javascript
- TDD and Javascript with JsMock
Best ongoing sources: Pathfinder Blog and Ajaxian seem to be good reading.
What tools did I miss? Are there any good JavaScript mailing lists where the participants discuss TDD?





Pingback: Interessante Links im Netz 11 | DaRaFF's Blog
Pingback: YUI unit testing - Programmers Goodies