What is Unit Testing and Why in Angularjs?

8 Sep by vichu

What is Unit Testing and Why in Angularjs?

Reading Time: 4 minutes

Unit testing is the concept of taking a single function or part of our code, and writing assertions and tests to ensure that it works as intended.



While very common on the server side, we often find the habit of writing unit tests amiss when developing clientside applications. Here are five reasons why we should write unit tests when working on a JavaScript-based client-side application:

Proof of correctness

Without unit tests, there is blind reliance on end-to-end manual tests to ensure that our feature or part of the application actually works. Unit tests act as proof that what we have developed actually does what it is supposed to, that we have handled all the edge cases correctly, and that it delivers the correct result in all these cases.

Lack of compiler

In JavaScript, there is nothing like the Java compiler to tell us that something doesn’t work. It is up to the browser to ultimately tell us that something is broken, and even then, different browsers produce different results. Unit tests can be run well before our application makes it out to the browser to catch problems and warn us about our assumptions.

Catch errors early

Without unit tests, we would only know about an error in our application after we hit refresh in our browser and saw the live application. Unit tests can help us catch errors much earlier, reducing turnaround time and thus increasing our development speed.

Prevent regressions

At the end of the day, we are unlikely to be the only person working on our codebase. Other developers will inevitably rely on or actively change parts of the codebase that we developed. You can ensure that they don’t change any fundamental assumptions by providing a set of unit tests that prevent regressions and bugs in the future.

Specification

Comments have a bad habit of becoming outdated. Unit tests in AngularJS, especially written using Jasmine, look and read like English. And because unit tests break when the underlying code changes, we are forced to keep comments updated. So unit tests can act as a living, breathing specification for our codebase.



Byvichu

Love to code and Love to learn. A passionate technology lover who likes to sit with laptop even for weeks :D

Leave a Reply

Your email address will not be published. Required fields are marked *