Developer

Unit testing options for JavaScript

Unit testing is different from traditional testing because it is performed by the developer and not a tester. Tony Patton provides an overview of several options for unit testing client-side JavaScript code.

 

Testing is an integral aspect of any software development project, but it is not restricted to the stage where a group of testers (who are not involved in application development) bang away on an application to make sure it meets expectations. Developers help ensure code performs as expected by integrating testing into their everyday work with unit testing. Here's a look at a few options for unit testing client-side JavaScript code.

Unit testing

Unit testing is the testing of individual software components or modules. Unit testing is different from traditional testing because it is performed by the developer and not a tester; this is due to the fact that knowledge of the code is required for unit testing. Later, traditional testing is accomplished with all modules (that have been individually unit tested) working together.

A key feature of unit testing is assertions; these allow the developer to check code execution and variable and object state during unit testing. Assertions allow you to perform common comparisons and condition testing to examine how code is running.

Many developers don't recognize JavaScript has real programming, but JavaScript is a full-featured language that has widespread usage given the popularity of AJAX, which relies heavily on JavaScript code. So, how does a developer unit test their JavaScript code? A quick survey of the available JavaScript unit testing options yielded a few options.

JsUnit

JUnit is a popular unit-testing tool for Java code. It was developed years ago, and it has become one of the more popular unit testing frameworks for Java developers. JUnit is based on the xUnit framework, which is a set of language-independent concepts and constructs for writing and running unit tests.

JUnit was so popular that NUnit was created for the .NET Framework. JavaScript developers can now take advantage of it as well with JsUnit for unit testing client-side JavaScript code. A great feature of these unit testing frameworks is that they are open source.

JsUnit is pure JavaScript; that is, it is written with 100% JavaScript. Like many Web applications, it runs on most browsers. The fact that it runs within the actual browser sets it apart from other testing options. The list of supported browsers includes the following:

  • Internet Explorer 5.5+ on Windows 95/NT/2000/XP and Mac OS 9/X
  • Mozilla 0.9.4+ on all platforms
  • Firefox 0.9+ on all platforms
  • Netscape 6.2.3+ on all platforms
  • Konqueror 5+ on Linux KDE 3.0.1+
  • Safari 1.2+ on Max OS X

A key aspect of JsUnit is that it is designed to test client-side JavaScript code and nothing else; so, you will not use JsUnit to fully test a Web application.

While JsUnit provides the same functionality as JUnit and NUnit, it utilizes its own vocabulary to describe its features. For example, JsUnit's unit tests are called Test Functions, which reside in Test Pages (an HTML file). A test runner page is used to run Test Pages.

JsUnit is a great tool; however, it has been a couple of years since there were product updates on its SourceForge page.

Crosscheck

Crosscheck is a Java-based tool for unit testing JavaScript code. It is a standalone application that does not run within a browser, so it is a bit hard to realize whether the code actually works in a browser. However, Crosscheck promises to simulate the various environments in which your scripts may run, so it simulates various browsers.

Since the browsers are simulated, you do not have to load the various browsers that you want to test against. Crosscheck's goal is to provide a mechanism for testing that JavaScript libraries will function as expected.

Tests are defined in a test file that sets up the tests to run, as well as other functions. It allows you to set up various elements of a test (such as HTML elements) and user actions (like clicking a button) to simulate user interaction. The output of using Crosscheck is a bit rudimentary, as it is generated via the command-line interface.

RhinoUnit

RhinoUnit is designed to work with ANT utilizing the Rhino engine (JavaScript via Java). It provides full unit testing features, as well as advanced options like ensuring a function is called and generated and checking variable scoping. An interesting feature is its strong integration with JSLint to validate JavaScript code.

Libraries

Various JavaScript frameworks include unit testing features. For instance, qooxdoo (which resembles JsUnit) includes a test runner for unit testing its classes. The qooxdoo Test Runner framework can also be used for non-qooxdoo code; it provides a GUI, a layer of infrastructure, and an interface for arbitrary test classes. This allows you to write your own test classes and take advantage of the environment.

The Dojo Toolkit also provides a robust unit testing environment. Its testing tool is called the Dojo Objective Harness; it facilitates the use of custom namespaces but requires special steps to utilize the test runner included within the Dojo Toolkit. Dustin Machi's SitePen blog post provides instructions for making the Dojo Objective Harness work.

What tools do you use?

In Web development, there is a constant state of flux as tools and techniques continue to evolve and be introduced. What tools do you use to implement unit testing or other chores in your everyday work? Share your experiences and recommendations with the Web Developer community.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

———————————————————————————————————————————-

Get weekly development tips in your inbox Keep your developer skills sharp by signing up for TechRepublic's free Web Developer newsletter, delivered each Tuesday. Automatically subscribe today!

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox