Print Shortlink

Unit testing JavaScript – I

Unit testing in JavaScript is an overlooked concept. With the increase in use of JavaScript in applications, testing JavaScript code has become an imperative task. A number of developers who use JavaScript, particularly jQuery ignore testing the JS code. They still resort to manual testing.

Requirements evolve/change almost everyday in projects, and so does your code. JUnit/NUnit test cases act as our guards that complain when the Java/.NET code behaves the way it shouldn’t. We use unit testing a lot (even if you don’t practice TDD) atleast for the sake of automating testing. Somehow this isn’t the case with JavaScript.

My experience with JavaScript in projects has always been unpleasant, because after the code base starts growing, things begin getting out of control. One small change in the functionality, you will be left starring at the code for hours not knowing whether the change you have made will affect other portions of the application or not. While we realize the importance of having unit tests in C# and Java applications, we don’t see the same in JavaScript.

If the UI of your application and communication and data exchange logic are completely implemented in JavaScript, the job will be a nightmare when you don’t have solid test cases backing up your JavaScript code.

There are a number of JavaScript test libraries like Jasmine, JSUnit, QUnit etc., which can be readily used in applications. I have been a fan of QUnit for it’s sheer simplicity. It has been of immense use particularly when jQuery is used.

In the next post, we’ll discuss writing test cases using QUnit.

Page 1 of 1

One Response

  1. Nitya

    Excellent article. Thanks for posting. Very thought-provoking and interesting.

Leave a Reply