Automated Unit Testing for JavaScript

March 3, 2010 by Patrik Henningsson in Monday School 3 Comments » ()

In our work towards Continuous Deployment and an improved approach to JavaScript we are missing one very important thing – automated unit testing for JavaScript. Currently we have have some tests written in TestCase that will run in the browser. But we need away to automate this without the need of a browser so we have started to look at some alternatives.

The solution we’re currently playing around with is Rhino + Env.js + QUnit.

Rhino is a JavaScript implementation written in Java and you can run it in a terminal, which harmonize with our aim for automation.

Env.js is a DOM implementation written entirely in JavaScript and will simulate the basic functions of a browser.

QUnit is simply the framework for the unit tests.

So far it looks very promising and it looks like we can do much of our testing in this environment. We will post more of our discoveries here later when we have done more testing, so stay tuned or follow me on twitter (@pahen).

  1. 3 Responses to “Automated Unit Testing for JavaScript”

  2. Since you use Ruby, you might also want to look at the Ruby/Spidermonkey fork of env.js at http://github.com/smparkes/env-js.

    By Steven Parkes on Mar 3, 2010

  3. Thanks for the tip but in fact we're using TestCase without using Ruby. But if we starting use Ruby, I'll definitely take a look at it.

    By pahen on Mar 4, 2010

  4. It would probably be good to start a javascript testing project in contrib (e.g. qunit), before trying to move it into core for Drupal 8. That way, we can have tests for Drupal 7 even if they're not in core, and it would be a great starting point for a JavaScript testing framework for Drupal 8.

    Also, I'd like to note that I no longer think that using qunit is the best approach. While qunit is a wonderful automated JavaScript testing framework, my understanding is that it's really designed to test JavaScript independent of web content, not JavaScript that depends on PHP-driven web content (such as collapsible fieldsets, sticky table headers, tabledrag, and nearly every other major JavaScript implementation in Drupal). I think we should work out the details of a testing framework in contrib before coming in with a core patch.

    By Bowmanave on Jan 20, 2011

Post a Comment