Introduction

Before moving further, let us look at the previous articles of the series

JavaScript

JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.

Unit tests

Unit testing is a software development process in which the smallest parts of the Application, called units, are tested. It can be done manually orthe process could be automated. Unit Testing is a level of software testing where the individual units/ components of the software are tested. The purpose is to validate that each unit of the software performs as designed.

Advantages of Unit tests

I see a lot of advantages in Unit tests, I have listed all of them as per my experience,
Unit tests and TDD go hand-in-hand. The concept of TDD is to first write a failing test, then we write enough code to pass the test and refactor the code. Generally, as a developer, we write fast code that works goodwell, but we don’t think to write Unit tests.
Source http://www.juancarbonel.com/
Unit tests are the safety nets and test whether the code is working or not by just running it.
A list of all the unit test frameworks is available here.
Few popular frameworks are:
Other concepts

Test runner

A CLI based tool requires you to run your tests. As developers, we shall love command-line interface and integrate our scripts with it. In an automation, it is of great help and we can integrate the test runner tool with the various environments too. There are many test runner software available. For e.g. Karma, Grunt, Gulp, Nrunner.

Assertion

It means to validate the condition or the output. We can determine the output; whether a test makes your code fail or pass depends on the assert condition. Different test suites/automation tools syntax can vary but the purpose is the validation, e.g. Assert.IsTrue (true);

Pre-requisites

You need to install NodeJS to enable the unit tests in JavaScript. You can refer to the article given below, by clicking it.
Let us do Unit testing now. To do that, we will install some node packages, i.e, Jasmine and Karma.
There are two ways to install things in NodeJS

Installing Karma and plugins

Karma install: fire below command to install karma,
npm install –g karma

Karma-Jasmine

Karma runner supports many different test frameworks like Jasmine, Chai & Mocha etc. We will install karma-jasmine module,
npm install -g karma-jasmine

Chrome launcher

Although we can leverage any supported Browser by Karma, I will show the tests via most popular Browser Chrome. Hence, install Chrome-launcher,
npm install –g karma-chrome-launcher

Karma CLI

It is a nice interface; from command line to leverage Karma, install,
npm install –g karma-cli
Write Unit Test Program

TDD principles

I am sure that you are familiar with TDD. In TDD, we promote first to write a test and then code. The above example was to quickly demonstrate you passing the test. Now, we will ensure the principles of TDD.
I committed this code at GitHub and from here you can download or clone it.

Summary

We can go more in-depth if we want by understanding Karma configuration, Jasmine, and other frameworks. I believe this article must have given you the context of Unit test in JavaScript. Please share your feedback/comments.