Stack OverflowJavascript Unit-testing?
[+159] [16] Peter Stone
[2008-08-28 17:02:23]
[ javascript unit-testing ]

What's the best way to go about running unit tests for Javascript? I've been playing around with Selenium IDE, but it's Firefox-specific. Selenium RC looks... difficult - but is it the best way to run tests in, say, IE6?

Also, is it worth doing? I haven't seen much about automated tests for Javascript applications, but it seems to me that as the language running most of the user experience it should be tested. Do you test your Javascript, just back end code, or neither?

[+85] [2008-08-28 17:12:44] Vitor Silva [ACCEPTED]

In the last couple of days I've also been looking for javascript unit-testing frameworks, but I still haven't decided on one.

  • This one, QUnit [1], looks interesting. QUnit is the unit testrunner for the jQuery project

  • Also, FireUnit [2], a FireBug extension, was recently launched. FireUnit provides a simple JavaScript API for doing simple test logging and viewing within a new tab of Firebug.


[+49] [2008-09-15 03:59:56] marcospereira

I already have tried both JsUnit and QUnit and I found QUnit is easier to use. Some good articles about QUnit:


[+26] [2010-08-11 00:25:06] dwfrank

If you like Screw.Unit, I recommend you switch over to Jasmine [1]. Jasmine is a BDD framework as well, but removes the dependency on jQuery that allows your tests to be run in a lot of other environments - Rails, PHP, Node.js, Palm webOS, and even iPhone (say PhoneGap).

Also, sadly, Screw.Unit is not being actively maintained - though there are some active forks of the project.

We wrote Jasmine for these (and a few other reasons). Check it out - we'd love your feedback.


(1) I'm using Jasmine and Jasmine-jQuery to test my app. So far it's been great! The Railscast tutorial on it is very helpful. - Nathan Long
[+15] [2008-08-28 17:09:10] Bill the Lizard

JsUnit [1] is a port of JUnit that allows you to run Javascript tests on multiple browsers. I do a lot more back end testing than front end, but I like to automate any test that I can.

Update: In response to leeand00's comment, there are several alternatives to JsUnit.

Just to name a few. There are more alternatives for different development environments [5] as well.


I like JSUnit too, but it doesn't seem to work with the latest version of Firefox 3. Is there an alternative? - leeand00
@Bill the Lizard Thanks for the alternatives! I did find a way to sort-of make it work with the exception of some browser cache… - leeand00
[+9] [2009-11-17 11:40:14] Alexander Klimetschek

Google's jsTestDriver [1] is also nice, especially for testing cross-browser.

In addition to a JUnit-like framework for writing the tests using assert*() methods, it provides an automatic test runner for quickly testing multiple browsers at the same time with just a "run" click from your IDE (Eclipse and IntelliJ are supported).

And there is an adapter for QUnit [2].


[+5] [2011-06-24 19:10:58] Matthew Manela

Chutzpah - A JavaScript Test Runner [1]

I created an open source project called Chutzpah which is a test runner for JavaScript unit tests. Chutzpah enables you to run JavaScript unit tests from the command line and from inside of Visual Studio. It also supports running in the TeamCity continuous integration server.


(2) You need to explicitly disclose your affiliation in the body of each post where you promote a product or project you work on. See the promotion section of the FAQ. - Bill the Lizard
(1) Thanks for the comment. I meant to write that I created it. I updated the post to reflect that. - Matthew Manela
[+3] [2008-09-17 14:38:48] Scott Bale

I've recently had success with Crosscheck a JS unit testing framework implemented in Java (on Mozilla Rhino engine).

On the plus side:

  • Crosscheck simulates different Browser DOM implementations (IE, Firefox)
  • Implemented in Java, runs tests in Java, does not require a browser to run tests
  • You can truly do JavaScript TDD, fast feedback loop
  • Maven plugin available on Google Group page, so you can integrate your tests into automated build
  • works with JS libraries (I used jQuery, Prototype)
  • project owners are pretty responsive on the Google group forum

On the minus side:

  • Crosscheck offers only a simulation of different browsers (but that gets you a long way)
  • Crosscheck is still pre-1.0
  • I had to tweak and rebuild the Maven plugin, it was not binary-compatible with the latest crosscheck release
  • Maven plugin not available in any public repository at this time

[+2] [2008-09-15 05:04:20] Till

Not quite unit testing, but you may also want to check out lint [1].


[+2] [2008-09-17 14:42:30] Matthew Wilson

JSUnit is difficult if you have anything asynchronous going on.

v3 of Yahoo's YUI test is easier in that case: [1]


(2) If you have anything asynchronous going on, it's not a unit test. - mehaase
[+1] [2008-09-15 02:39:11] brass-kazoo

Selenium RC is mostly used for testing the webpage layout and DOM, although it does provide a getEval() [1] function that you can use to explicitly test javascript expressions.

From the Selenium IDE in Firefox you can export the recorded testcase to the code to the language of your choice, and I think the Selenium doco and tutorial [2] should be enough to get you started with the RC server.

Its an extremely useful tool if you want to also test your page layout and simulate user interactions.

However, if you're purely testing javascript, then JsUnit (as suggested by Bill [3]) is probably a better option.


[+1] [2008-09-26 16:18:25] slashnick

JSSpec [1] is an alternative to JSUnit with a behavior driven development bent to it. It looks a bit more javascript-y to my eye.


[+1] [2010-07-01 22:02:29] alshur

jsunit rocks! it's kinda old-fashined, but stable.

[+1] [2011-02-21 04:13:03] itechp2pexchange javascript library having unittest.js script. Using this we can perform the javascript unit testing.

To write unit test, just include the unittest.js and write test script for each function. It have assert, assertEqual,assertNotEqual, assertIdentical and so on options to test your scripts.

get more info from

Thanks a lot! I knew that Mad Robby made some kind of unit testing framework, but it was not trivial to find it :) - Vadim
[0] [2008-08-28 17:07:54] Joel Martinez

look at Watin [1] if you use visual studio


It won't help you with unit-testing. It's a browser automation tool - Alexander Abramov
[0] [2010-08-03 11:30:17] user409594

I have just been assessing a few, and so far YUI Test been using Screw.Unit [1] have come on top. Screw.Unit is nice because there is a maven plugin for it, and is based on Behavior Driven Development rather than jUnit style assertions. On the minus sites, the maven plugin uses env.js, which is quite buggy (it won't run with Prototype.js for example). YUI Test is a very nice library, but automation seems to be in its early stages - although there seem to be some Selenium integration with YUI Test (there is a link to it but SO won't let me post it)


[0] [2011-12-16 21:56:23] col

I think should be considered. It's still in beta, but has great features already. Potential for unit testing node.js or browser.