Unit Testing in JavaScript
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?
Related posts:
November 26, 2008 at 2:02 am | Nicholas C. Zakas
Just FYI, a mock object utility is in the works for YUI Test and will be available as part of YUI 3.0 Preview Release 2. If you have any questions about YUI Test, please check out the YUI discussion list or email me directly.
November 26, 2008 at 6:04 pm | Nathan L Smith
Scriptaculous also includes a basic unit testing framework: http://github.com/madrobby/scriptaculous/wikis/unit-testing
It’s also included in Prototype’s repository.
November 26, 2008 at 10:49 pm | kky
take a look this one:
Flexible Javascript Unittest Framework –
http://code.google.com/p/flexible-javascript-unittest/
November 28, 2008 at 2:26 am | masya
I think you’ve missed THE testing tool – Selenium. Take a look here:
http://extjs.com/blog/2008/11/03/testing-ext-js-ext-gwt-applications-with-selenium/
November 28, 2008 at 7:26 am | choan
There’s a port of the Scriptaculous/Protototype testing framework with no dependencies on prototype. JsUnitTest: http://jsunittest.com/
On top of that, you can use jShoulda (http://jshoulda.scriptia.net/) that provides nexted contexts and other niceties.
November 28, 2008 at 9:40 am | Mark Levison
maysa – I’m familiar with Selenium but thought it was really a functional testing tool and not a unit testing tool. Did I miss something critical?
choan, kky, Nathan and Nicholas thanks for the additional frameworks.
November 28, 2008 at 3:04 pm | Andreas Ecker
You might also be interested in the unit testing framework that is part of qooxdoo, an open source RIA framework similar to Dojo, ExtJS, etc. but with quite a comprehensive tool chain.
Try the online version of the testrunner. Hint: hit the gray arrow at the top left (should be made more obvious). It’s a “play” button that runs the selected tests.
To find out more about the JS classes that let you define your unit tests, see the online API viewer.
For automated UI testing (based on Selenium RC), check out the Simulator project.
December 1, 2008 at 8:56 am | Jay Kimble
One you missed is excmaunit… sort of jsunit, but in browser.
I’d give you the URL, but I’m not sure of it… (google it yourself)
December 1, 2008 at 11:25 am | Jay Kimble
BTW, I fat fingered that… it’s EcmaUnit –> http://kupu.oscom.org/download/ecmaunit-0.3.html
Last update is a long time ago, so if you want something recent this isn’t the animal for you
January 2, 2009 at 11:08 am | Radek Maciaszek
There is a fork of JSUnit which doesn’t seem to have all the cons you mentioned (can be run using Rhino, doesn’t have to embedded into Html and seems to have some recent releases): http://jsunit.berlios.de/
January 6, 2009 at 9:17 am | Tiest Vilee
hi!
I’m lead developer for RhinoUnit, and I’d love to know how I can improve the library for you. If you could let me know what problems you had I could look into fixing them for you.
You mention that you tried to run test code from JSUnit in it – this isn’t really how RhinoUnit works. I’ve tried to make it as javascript fluent as possible, rather than trying to mimic SUnit. Consequently I use Functions rather than objects, and I test for more JS specific things like forgetting VARs, running JSLint, ensuring that functions get called an so on.
Basically the framework developed out of real world requirements rather than just copying someone elses.
January 6, 2009 at 9:23 am | Mark Levison
Tiest – In fact I wasn’t the one who had done the work testing the frameworks, I was just doing research on behalf of a colleague who had. I’ve passed your notes along to him.
February 8, 2009 at 5:07 am | Korny
A great list – very useful.
Another mocking library I discovered – amok http://code.google.com/p/amok/ – just because you don’t have enough options already!
February 15, 2009 at 6:38 pm | Jon Kruger
I’ve used JSSpec and it worked pretty good for me.
I wrote up a series of posts about writing JavaScript tests – http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-introduction-javascript-basics/.
Here is a good post on integrating QUnit tests with TeamCity builds… http://www.lostechies.com/blogs/joshuaflanagan/archive/2008/09/18/running-jquery-qunit-tests-under-continuous-integration.aspx
April 11, 2009 at 3:12 pm | TJ
JSpec has come along way, its syntax is far better than the rest, check it out http://gist.github.com/92283 http://visionmedia.github.com/jspec/
July 17, 2010 at 12:29 pm | Christian Williams
Also check out Jasmine — http://github.com/pivotal/jasmine — an easy, framework-agnostic behavior-driven development environment for JavaScript.