1. Computing

JavaScript Testing

Commenting Out Code to Aid Testing

From , former About.com Guide

If you have a large and complex JavaScript that isn't working correctly then the last thing you really want to do is to try to debug the entire script all at once. Debugging will be far more manageable if we can divide the script up into pieces and to test each piece separately. As we determine that each separate piece of the script works we can narrow down the area of the script where the problem lies that is causing the script to not work as required.

The simplest way to divide and conquer with JavaScript is to comment out sections of the script. Just exactly how we do this will depend on what the pieces of code do and just what each individual test is trying to determine.

Where it appears that our script is not running at all it usually means that there is a syntax error somewhere inside a function that is causing the script to not recognise where the function ends. We can test if a particular function is the cause of this sort of error by wrapping all of the content of the function inside a comment. If the function is supposed to be returning a value then we can temporarily add a return statement that returns a specific value that while not necessarily the one that the function will always return will at least be sufficient to allow the following code to continue running. If the script now runs then you have identified that the syntax error that is causing the script to not work is within the code that you have commented out. You can then adjust what part of the function is commented to help you narrow down which statement within that code is the actual cause of the problem.

Commenting out blocks of code can also be used to make it easier to unit test specific parts of the code.To thoroughly test a particular function within the code you might comment out all of the code in the script except for that function and then temporarily add a number of test calls to the function passing it different values and if necessary using an alert to display the value that each call returns. That way you can check that one function for lots of different alternatives without needing to run all of the rest of the script at the same time. This is particularly useful where given values into the function may seldom occur when the rest of the script runs.

Yet another use for commenting code during testing is where you are testing a script that is supposed to update something each time that it is run and where subsequent runs will therefore normally produce different results. In this situation commenting out the particular piece of the code that performs that update will allow the same test to be run over and over without your having to reset the updated value back in between tests.

The only thing that you have to be careful about if you comment out pieces of the script while testing is to make sure that you uncomment all of the code and remove any temporary statements once you complete those specific tests. You don't want those temporary changes to be the cause of errors in the script.

Top Related Searches syntax error unit test

©2013 About.com. All rights reserved.