JS Unit Testing

JS Test Driver

http://code.google.com/p/js-test-driver/
http://msdn.microsoft.com/en-us/scriptjunkie/gg650426 (older verson of sinon)

Test files are JS files. A config file brings in other required files.

Make sure you get 1.1.1e, not 1.1.1c

Pros

  • eclipse plug-in
  • put JS src, test files wherever you like
  • no need to create HTML page pulling in all required javascript files
  • easy to set up testing on different browsers (although testing IE can’t be done in a CI server that isn’t using Windows)
  • runs http server for you, although for integration with real AJAX services, the other server will need to run
  • there is a maven plug-in but it seems to have some trouble with Chrome browser windows
  • won’t easily work with selenium (though these two solutions are not complementary)
  • missed some test failures due to errors like missing javascript files
  • can still debug but somewhat trickier

Cons

  • no HTML file, so DOM needs to be written first

QUnit

with sinon

Test files are HTML pages which include required javascript and DOM elements.

Pros

  • looks nice
  • easy to integrate with mock library (sinon)
  • eclipse integration can be simulated using a JS Test Runner plug-in
  • there are plug-ins

Cons

  • lacking multi-browser testing – although you could use Selenium
  • need to create DOM yourself

YUI Test

  • http://developer.yahoo.com/yui/3/test/

Cons

  • very verbose when writing tests – don’t just write tests but also suites, then they must be run manually

Jarvis

Cons

  • errors logged to console, not page

JS Class

  • http://jsclass.jcoglan.com/testing.html

Cons

  • requires re-writing code to use JS Class library. In this library, every class inherits from JS.Class. It looks pretty heavyweight.

JSpec

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

Pros

  • has mocks, fakes
  • BDD style

Cons

  • more for RoR
  • command-line driven
  • automation not easy

Jasmine

Jasmine Maven Plug-in

Selenium

Selenium, cargo and maven : http://www.baeldung.com/2011/10/16/how-to-set-up-integration-testing-with-the-maven-cargo-plugin/

Pros

  • can run all browsers

Cons

  • slow
  • need to custom-write code that will tell you which tests failed

Sinon

Test Swarm

  • distributed continuous integration server

Pros

  • test-suite agnostic

Cons

  • requires dedicated PHP/MySQL server

Phantom JS

Headless js

  • http://www.phantomjs.org/

with maven integration

Pros

Cons

  • doesn’t allow cross-browser testing, because no browser is used (although it is based on WebKit, which is used by Firefox, Safari and Chrome)
  • … though you can develop the tests in any browser (including IE) so this shouldn’t be too much of an issue

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply