software solutions / project leadership / agile coaching and training

Unit Testing JavaScript - JavaScript test frameworks

Posted on November 25, 2008 in JavaScript, TDD, unit testing

In my last post I gave you some basic information on JavaScript that will help you get started on the road to testing your JavaScript. Now for the JavaScript test frameworks.

JavaScript test frameworks

I did some research and there are quite a few JavaScript test frameworks out there:

JSSpec J3Unit
QUnit JSUnit
YUI Test Screw-Unit
JSNUnit script.aculo.us unit testing
TestCase Crosscheck
RhinoUnit jqUnit

So apparently there are people that are trying to solve this problem, but how do you choose between all of these frameworks?

When I want to determine the relevance of something, I go straight to search.twitter.com. I figure that if no one is talking about a JavaScript framework on Twitter or if something is getting a lot of negative comments, then it’s probably not worth looking into.

Using my completely unscientific Measure of Relevance I was able to narrow down my list of JavaScript testing frameworks to the following list:

JSSpec YUI Test
QUnit Screw-Unit

I chose JSSpec because it uses the behavior driven development terminology that I’ve become accustomed to recently. Screw-Unit also is BDD-based but it didn’t seem to be as straight-forward. QUnit is a newer framework that was originally designed by the jQuery folks as a framework for testing jQuery. I used QUnit on a real project for a client and it got the job done. QUnit seems to be the most talked about of the frameworks just because people find it when their on the jQuery site. (FWIW, QUnit doesn’t have any integration with jQuery that makes it any easier to use than the others.) YUI Test is used with the Yahoo UI framework, so if you’re using the YUI framework, YUI Test may be a good fit for you. People that use YUI Test seem to have a lot of good things to say about it.

JavaScript mocking frameworks

JavaScript mocking frameworks are not as prevalent, partly because it’s really easy in JavaScript to redefine any method in any class to be whatever you want, so you don’t need a mocking framework to redefine methods. But it’s still nice to have a framework that lets you verify that certain methods were called. If you want to look into mocking frameworks, Jack is a good place to start. It’s still in alpha (at the time of this post) but it looks usable to me. It integrates with JSSpec which is another plus.

Next up: writing testable JavaScript.

5 Comments »

  1. [...] Unit Testing JavaScript - JavaScript test frameworks [...]

    Jon Kruger’s Blog » Blog Archive » Unit Testing JavaScript - Writing testable code — November 25, 2008 @ 6:52 pm

  2. [...] Unit Testing JavaScript - JavaScript test frameworks [...]

    Jon Kruger’s Blog » Blog Archive » Unit Testing JavaScript - Introduction, JavaScript basics — November 25, 2008 @ 7:07 pm

  3. Hi,

    I noticed that the JS testing tools you’ve chosen seem to be browser based. I developed RhinoUnit specifically for running from the command line so it can be easily incorporated into cruise, or whatever continuous integration tool you use.

    I agree with your observation about mocking in JS, but there are a few things that can be useful. RhinoUnit provides a ‘function that must be called’ to ensure that your code does actually call something. It also checks for variables that accidently make it into the global namespace.

    Anyhoo, we’ve found it quite useful, and I’d be open to any suggestions about how to make it better.

    Tiest Vilee — January 6, 2009 @ 7:51 am

  4. @Tiest,

    Thanks for the tip, I’ll have to check that out.

    Jon Kruger — January 6, 2009 @ 9:22 am

  5. JSpec is my fav by far, looks way better, and has way more matchers

    http://visionmedia.github.com/jspec/

    Mark — April 11, 2009 @ 2:17 pm

Leave a comment





SERVICES
SOFTWARE SOLUTIONS
I have over 10 years of software development experience on several different platforms (mostly Ruby and .NET). I recognize that software is expensive, so I'm always trying to find ways to speed up the software development process, but at the same time remembering that high quality is essential to building software that stands the test of time.
PROJECT LEADERSHIP
I have experience leading and architecting large Agile software projects and coordinating all aspects of a project's lifecycle. Whether you're looking for technical expertise or someone to lead all aspects of an Agile project, I have proven experience from multiple projects in different environments that can help make your project a success.
AGILE COACHING
I believe that Agile processes and tools should be applied with common sense. I've spent the last 6 years working on Agile projects as a consulant in many different environments, both in leadership roles and as a practitioner doing the work. I can help you find out how Agile can work best in your organization, not just apply a prescriptive process.
TEST DRIVEN DEVELOPMENT TRAINING
TDD Boot Camp is a hands-on, three day, comprehensive training course that will teach you all of the skills, tools, frameworks that you will need to use test-driven development to develop real world .NET applications. If you're not looking for something that intensive, check out the the half-day version.
Have any questions? Contact me for more information.
PRESENTATIONS
(presented with Paul Bahler and Kevin Chivington from IGS Energy)
From CodeMash 2011
An idea of how to make JavaScript testable, presented at Stir Trek 2011. The world of JavaScript frameworks has changed greatly since then, but I still agree with the concepts.
A description of how test-driven development works along with some hands-on examples.
From CodeMash 2010
From CodeMash 2010