Browserless JavaScript Testing

As users expect more sophisticated AJAX, animation and event functionality, the amount, and complexity of Javascript code is increasing. Even small sites and RIAs can contain 100’s of lines of Javascript code. With the increase in complexity comes the potential for bugs.  Writing clean code matched with lots of testing is what’s required to keep projects on track.

With unit testing we can test individual units of code to determine if it can be relied on in an application. Unit testing is very common in software development but less common in web development.

To run these units tests outside the browser I use a Javascript interpreter that runs the code and the tests. There are many alternatives out there like Rhino, PhantomJS, SpiderMonkey (the engine behind Firefox) and V8 (the engine behind Chrome). These engines are great because they allow you to run Javascript code on the server or command-line but they do not have built-in capabilities to manipulate HTML documents or the DOM.

Below is a quick transcript of the steps I took to unit test a function “validateEmail” which validates an email address by applying a regular expression to the input.

//on validateEmail.js
function validateEmail(email) {
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
return filter.test(email);

print(" "+validateEmail(""));
print(" "+validateEmail(""));

//on console

//this will generate the the class file (validateEmail.class)
java validateEmail.js

//run the java
java validateEmail

//will output true false

A headless browser is a browser without a GUI but it’s capable of parsing Javascript, “clicking” on links, load images, determining text color, etc. All these can be done programatically from a console, so it can be used to run tests on the server as well.  I’ve been working with EnvJS, but there are alternatives like ZombieJS. The code below shows how to use Rhino and EnvJS to load a page on a console and run a series of tests.


QUnit.config.blocking = false;
QUnit.config.autorun = true;
QUnit.config.updateRate = 0;
QUnit.log = function(object) {
print(object.result ? 'PASS' : 'FAIL', object.message);

//load html to test
window.location = "../index.html";


To run all this tests in the server automatically you can use Hudson, which is a continuous integration server. Hudson will check your source repository for a new version of the code every few minutes and build a the project if necessary.  And of course, it will run all your unit tests and report back to you if any fail.

Working this way can save you headaches later by finding some of the errors earlier in the process.


  1. Unit testing
    1. Qunit –
    2. JSAssertUnit –
    3. JSUnit –
    4. RhinoUnit –
    5. Others:
  1. Javascript Engine
    1. Rhino –
    2. PhantomJS –
    3. SpiderMonkey –
    4. V8 –
  1. Console browser
    1. EnvJS –
    2. ZombieJS –