How to perform JavaScript Unit Testing

Following the TDD approach when you are working with MVC it is quite easy to produce Unit Tests that are based on the MVC Controllers, with more and more business logic appearing in the User Interface by using libraries such as jQuery it is becoming more important to perform Unit Testing on JavaScript.

What options are available today, more importantly what are the pros and cons of each solution?

JsUnit

Pros

    can be invoked from an ant build file
    launches browser to run the tests
    Eclipse plug-in

Cons

    launches browser to run the tests
    Does not support js file to write the unit test code: it has to be embedded inside an html file
    it has not been updated for a few years

Notes:

    There is a JsUnit (2).
    An ‘ant’ is an open source build tool; “Ant” because it is a little thing that can build big things.

RhinoUnit

Pros

    ant driven
    supports js file
    very simple to use

Cons

    Simulation of JavaScript engine: not advanced enough to support all coding types

crosscheck

Pros

    Can be invoked from ant build file
    Simulates real browser behaviour

Cons

    Simulation of JavaScript engine from a limited number of browser versions
    No activity for 2 years: it does not support Firefox versions 2.x nor 3.x

jsspec

Pros

    Runs on actual browser

Cons

    JavaScript only framework: cannot be called from ant build file

jspec

Pros

    Runs on actual browser

Cons

    Does not seem to support all code types
    JavaScript only framework: cannot be called from ant build file,

Screw.unit

Pros

    Runs on actual browser

Cons

    JavaScript only framework: cannot be called from ant build file

JSTest

Pros

    Can be intergrated with most testing frameworks, such as MSTest, NUnit, xUnit etc
    ant build supported
    Simple to install, only requires a single dll
    Browserless
    Can be installed using NuGet
    Small overhead of 56k

Cons

    Not very active on CodePlex, with only 51 downloads
    Reference directly to the js files, so if the js files are moved all the tests need to be updated

I know there are many many more and this is just a cross sample of JavaScript testing libraries, but I think I may have found the crown jewels for TDD.

It looks like JSTest is, not only the only choice we have, it is the simplest and best approach to JavaScript Unit Testing, but why has it only had 51 downloads?  Have I found a hidden gem or a monster waiting to bite me?

JSTest does provide an easy way to apply the Test-Driven Development (TDD) process

For an example to use it check out Unit Testing JavaScript with MSTest and JSTest.Net by Shawn Sweeney, I have created a sample MVC application showing it working.

JSTestMVCExample.zip (2.67 mb)

Conclusion, I do think we now have the tools to be fully “TDD” compliant.

JSTest.zip (16.80 kb)