1. Computing & Technology

JavaScript Testing

Types of Testing

From , former About.com Guide

There are a range of different types of test that you can run in order to verify that your JavaScript code works correctly.Which of these types of tests that you will need to run depends on just what your script is intended to do and what other scripts there may be on the same page. It also depends on what sort of visitors you expect to get to the site.

Unit Tests

This sort of testing is where you take a small piece of JavaScript (perhaps a function or method) and test it in isolation - separately from the rest of the page. In this way you can ensure that the code does what it is supposed to do. This then means that if the code doesn't do what is expected when it is included in the web page that there is code elsewhere in the page that is interfering.

Web Page Integration Tests

Here you take a script that you know works in isolation and plug it into the web page in which it is to be used and then test that it still works there and doesn't have conflicts with anything else that is in the web page.

Regression Tests

When you make changes to a part of a script you need to make sure that the changes don't interfere with the operation of the parts that you didn't change. Regression testing reruns the original tests of that part of the code that is unchanged in order to ensure that those parts of the code still give the same results as before.

Performance Tests

This sort of testing doesn't look at whether the code produces the correct results or not since it is assumed that the testing that has already been run has covered that. What this testing looks at is how long it takes to produce the results. JavaScript performance testing is a lot different from server side performance testing because on the server the main impact on performance will be the number of separate people making requests at the same time. JavaScript runs in the browser and so you know that you only have the one person involved. JavaScript performance testing is therefore concerned more with checking that the code you are using can run efficiently in all the major browsers. For example placing four analog clocks on a web page may run perfectly in some browsers but the volume of updates might result in the second hand jumping in browsers that have a less efficient implementation of JavaScript.

Usability Testing

Probably the main thing to test here with regard to JavaScript is whether or not the page is still usable if JavaScript is turned off.Depending on what code you use in your script you may also need to check if the script works where new web pages are set to open in a new tab rather than a new window, where being able to size or resize windows is disabled, and any of the othersettings that the various browsers provide in their configuration settings for what JavaScript is and idn't allowed to do.Another consideration if you use alert, confirm, or prompt in a live script is what happens if your visitor is using Opera and selects the option at the bottom of the dialog to abend the JavaScript at that point.

©2012 About.com. All rights reserved.

A part of The New York Times Company.