share
Stack OverflowJavascript Unit-testing?
[+159] [16] Peter Stone
[2008-08-28 17:02:23]
[ javascript unit-testing ]
[ http://stackoverflow.com/questions/32809/javascript-unit-testing ] [DELETED]

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.

[1] http://qunitjs.com/
[2] http://ejohn.org/blog/fireunit/

1
[+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:

[1] http://www.lostechies.com/blogs/chad_myers/archive/2008/08/28/getting-started-with-jquery-qunit-for-client-side-javascript-testing.aspx
[2] http://www.eviltester.com/index.php/2008/06/06/test-driven-javascript-using-qunit/
[3] http://www.compendiumdev.co.uk/blog/index.php/2008/06/18/clicking-the-buttons-in-qunit-functional-testing-with-jquery/

2
[+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] http://pivotal.github.com/jasmine

(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
3
[+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.

[1] https://github.com/pivotal/jsunit
[2] http://thinkpond.org/articles/2008/jstest-intro.shtml
[3] http://fireunit.org/
[4] http://www.thefrontside.net/crosscheck
[5] http://www.opensourcetesting.org/unit_javascript.php

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 problems...stackoverflow.com/questions/619170/… - leeand00
4
[+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].

[1] http://code.google.com/p/js-test-driver/
[2] http://code.google.com/p/js-test-driver/wiki/QUnitAdapter

5
[+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.

[1] http://chutzpah.codeplex.com/

(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
6
[+3] [2008-09-17 14:38:48] Scott Bale

I've recently had success with Crosscheck http://www.thefrontside.net/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 http://groups.google.com/group/crosscheck, 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

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

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

[1] http://javascriptlint.com/

8
[+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: http://developer.yahoo.com/yui/yuitest/ [1]

[1] http://yuilibrary.com/yui/docs/test/

(2) If you have anything asynchronous going on, it's not a unit test. - mehaase
9
[+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] http://release.openqa.org/selenium-remote-control/0.9.0/doc/java/com/thoughtworks/selenium/DefaultSelenium.html#getEval(java.lang.String)
[2] http://selenium-rc.openqa.org/tutorial.html
[3] http://stackoverflow.com/questions/32809/javascript-unit-testing#32826

10
[+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] http://jania.pe.kr/aw/moin.cgi/JSSpec

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

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


12
[+1] [2011-02-21 04:13:03] itechp2pexchange

script.aculo.us 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 http://madrobby.github.com/scriptaculous/unit-testing/


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

look at Watin [1] if you use visual studio

[1] http://www.google.com/url?sa=t&source=web&ct=res&cd=1&url=http%3A%2F%2Fwatin.sourceforge.net%2F&ei=bdu2SOPiAqS6NNiu0JYD&usg=AFQjCNHXfSg5X8F--n-t45vRYzO83m-r-w&sig2=rEvWVIQE4Gs2c9yZNcBHug

It won't help you with unit-testing. It's a browser automation tool - Alexander Abramov
14
[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)

[1] http://github.com/nkallen/screw-unit

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

I think http://busterjs.org/docs/overview/ should be considered. It's still in beta, but has great features already. Potential for unit testing node.js or browser.


16