1. Computing

JavaScript Testing

There's a lot more to testing JavaScript than just fixing the obvious bugs in your code. While the amount of testing required is dependent on how complex your script is and many scripts don't require a huge amount of testing, it is still worth at least learning a little about the alternative ways to test so you can select the appropriate way when you need to.

What is Testing?
There is only one way to find out if a script you have written will work properly. You test the script to see if it produces the expected results for a variety of alternative situations.

Types of Error
There are a number of different types of error that can occur with your JavaScript. Some types of error are easier to detect than others.

Common Javascript Errors
Commonly made Javascript errors and how to find them

Ad Hoc Testing
Ad hoc testing of JavaScript is where you test your script without having a formal test plan.

Debugging Javascript
Different browsers provide different options for locating Javascript errors.

Debugging Using Alert
The one way in which it is still acceptable to use the alert dialog in JavaScript is as a debugging aid.

Commenting Code to Aid Testing
By commenting out part of your script you can narrow down the area of the script that contains a problem.

Timing Errors
One common cause of errors where there is nothing actually wrong with your JavaScript code is timing. If your code runs too soon or too late it may not be able to do what you want it to.

Validating and Testing
Validating your JavaScript is not the same as testing it however validation can serve to detect the cause of some not so easy to spot errors.

Colour Coding Editors
Having your editor colour code your JavaScript can make it easier to visually spot errors.

Formatting and Compressing
For your script to run you want the code compressed/minified as much as possible (but without requiring any script to decompress it before it can run). For actually working on the script and doing the testing a nicely formatted version with plenty of whitespace is better.

HTML Validation And JavaScript
Errors in your HTML can cause your JavaScript to not work correctly.

Validation Warnings - Should You Fix Them?
Having run your JavaScript through the validator just what should you do about any warnings that are produced?

Why Ad Hoc Testing May Not Be Enough
With anything more than the simplest of JavaScript any ad hoc testing is almost certain to miss testing everything.

Do You Need A Test Plan to Be Able to Test JavaScript?
Most of the JavaScript that people write is a lot shorter and simpler than you are likely to find in application programs. The amount of effort...

When To Develop A Test Plan
The earlier you develop a test plan for your javaScript the more useful it will be and the sooner you will be able to fix errors.

Black Box or White Box Testing
There are two different approaches to testing your code - they are called black box testing and white box testing. Here we look at how these two approaches work and how to apply it to JavaScript.

Types of Testing
There are a variety of different sorts of testing that can and in many cases should be carried out on your JavaScript.

Deciding What to Test
Not all of the paths through your script are necessarily equally likely to occur. How unlikely a particular path is does not necessarily affect your testing.

Expected and Unexpected Values
When doing thorough JavaScript testing you not only need to test that expected values produce the expected result but also that unexpected values will produce an acceptable result.

Testing Order
Which order you run your tests in will affect how likely it is that you will need to rerun lots of tests if one fails.

The JavaScript Debugging Console
All but one of the popular browsers have a built in JavaScript debugger and the one exception can be easily modified to add one.

JavaScript Testing Tools
A selection of advanced testing tools for JavaScript

JavaScript Testing
When you are setting up tests for your JavaScript you need to consider what sorts of data are both valid and invalid as inputs and organise tests for both.

©2014 About.com. All rights reserved.