Book Image

JavaScript Unit Testing

By : Hazem Saleh
Book Image

JavaScript Unit Testing

By: Hazem Saleh

Overview of this book

<p>The largest challenge for many developers’ day to day life is ensuring the support of, and assuring the reach of, their product. With the ever increasing number of mainstream web browsers this is becoming a more difficult task for JavaScript coders. <br /><br />From the beginning, JavaScript Unit Testing will show you how to reduce the time you spend testing, and automate and ensure efficiency in guaranteeing your success.<br /><br />JavaScript Unit Testing will introduce and help you master the art of efficiently performing and automating JavaScript Unit tests for your web applications.<br /><br />Using the most popular JavaScript unit testing frameworks, you will develop, integrate, and automate all the tests you need to ensure the widest reach and success of your web application.<br /><br />Covering the most popular JavaScript Unit testing frameworks of today, JavaScript Unit Testing is your bible to ensuring the functionality and success of all of your JavaScript and Ajax Web Applications.<br /><br />Starting with Jasmine, you will also learn about, and use, YUITest, QUnit, and JsTestDriver, integrate them into your projects, and use them together to generate reports.<br /><br />Learn to automate these tools, make them work for you, and include the power of these tools in your projects from day one.</p>
Table of Contents (12 chapters)

Complexities in testing JavaScript applications


Testing JavaScript applications is complex and requires a lot of time and effort. Testing JavaScript applications requires the tester to test the application on different browsers (Internet Explorer, Firefox, Safari, Chrome, and so on). This is because the JavaScript code that runs on a specific browser will not necessarily work on another browser.

Testing existing JavaScript web applications (with many web pages) on new browsers that are not supported by the application code is not a flexible process. Supporting a new unsupported browser means allocating more time for testing the application again on this new browser and for the new/regression defects to be fixed by the developers. Let's see a simple Broken JavaScript example, which illustrates this idea. In this example, the user enters his/her name and then clicks on the Welcome button. After that the welcome message appears.

The following code snippet shows the broken JavaScript example:

<!DOCTYPE html>
<html>
<head>
  <title>Broken JavaScript Example</title>
  <script type=»text/javascript»>
    function welcome() {
      var userName = document.getElementById(«userName»).value;
      document.getElementById(«welcomeMessage»).innerText = «Welcome «   + userName + «!»;
    }
  </script>  
</head>
<body>
  <h1>Broken JavaScript Example</h1>
  
  <label>Please enter your name:</label>
  <input id=»userName» type=»text» /><br/>
  <input type=»button» onclick=»welcome()» value=»Welcome»></input><br/><br/>
  <div id=»welcomeMessage»/>

</body>
</html>

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

If you run the code shown in the previous code snippet, you will find that it works fine in Internet Explorer (IE) and Safari while it does not work in Firefox (to be more specific, this example works on Internet Explorer 8 and Safari 5.1, while it will not work on Firefox 3.6). The reason behind this problem is that the innerText property is not supported in Firefox. This is one of the hundreds of examples that show a code that works in a specific browser while it does not work in another one.

As a result of these complexities, testing JavaScript code requires a good unit testing tool, which provides mechanisms to overcome these complexities. The good JavaScript unit testing tool should be able to execute the test cases across all of the browsers, should have an easy setup, should have an easy configuration, and should be fast in executing the test cases.