Node JS - Node js Unit Testing | Node.js Unit Testing Tutorial with Jasmine - Node - node js - node js tutorial




What is Node.js Unit Testing ?

  • Testing is a key element to any application. For Node.js, the framework available for Testing is called Jasmine. In early 2000, there was a framework for testing JavaScript applications called JsUnit. Later this framework got upgraded and is now known as jasmine.
  • Jasmine helps in automated unit testing, something which has become quite a key practice when developing and deploying modern day web applications.
  • In this tutorial, you will learn how to get your environment setup with jasmine and how you can start testing your first Node.js application with jasmine.
 node-js  unit testing

Learn Node js - node js Tutorial - node-js unit testing - node - Node js Examples

Overview of Jasmine for testing Node.js applications:

  • Jasmine is a Behavior Driven Development(BDD) testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus, it's suited for websites, Node.js projects, or anywhere that JavaScript can run. To start using Jasmine, you need to first download and install the necessary Jasmine modules.
  • Next you would need to initialize your environment and inspect the jasmine configuration file. The below steps show how to setup Jasmine in your environment.

Step 1:

  • Installing the NPM Modules
  • To use the jasmine framework from within a Node application, the jasmine module needs to be installed first. To install the jasmine-node module, run the below command.
  • npm install jasmine-node
Related nodejs article tags - node js - node js tutorial - node js examples

Step 2:

  • Initializing the project - By doing this, jasmine creates a spec directory and configuration json for you. The spec directory is used to store all your test files. By doing this, jasmine will know where all your tests are, and then can execute them accordingly. The JSON file is used to store specific configuration information about jasmine.
  • To initialize the jasmine environment, run the below command
  • jasmine init

Step 3:

  • Inspect your configuration file. The configuration file will be stored in the spec/support folder as jasmine.json. This file enumerates the source files and spec files you would like the Jasmine runner to include.
  • The below screenshot shows a typical example of the package.json file for jasmine.
 nodejs example package

Learn Node js - node js Tutorial - nodejs example package - node - Node js Examples

  • Note that the spec directory is specified here. As noted earlier, when jasmine runs it searches for all tests in this directory.
  • The next thing to note is the spec_files parameter - This denotes that whatever test files are created they should be appended with the 'spec' keyword.

How to use Jasmine to test Node.js applications:

  • In order to use Jasmine to test Node.js applications, a series of steps needs to be followed.
  • In our example below, we are going to define a module which add 2 numbers which need to be tested. We will then define a separate code file with the test code and then use jasmine to test the Add function accordingly.

Step 1:

  • Define the code which needs to be tested. We are going to define a function which will add 2 numbers and return the result. This code is going to be written in a file called "Add.js."

Sample Code:

var exports=module.exports={};
exports.AddNumber=function(a,b)
{
return a+b;
};

Code Explanation:

 nodejs add number result

Learn Node js - node js Tutorial - nodejs add number result - node - Node js Examples

  1. The "exports" keyword is used to ensure that the functionality defined in this file can actually be accessed by other files.
  2. We are then defining a function called 'AddNumber.' This function is defined to take 2 parameters, a and b. The function is added to the module "exports" to make the function as a public function that can be accessed by other application modules.
  3. We are finally making our function return the added value of the parameters.
Related nodejs article tags - node js - node js tutorial - node js examples

Step 2:

  • Next we need to define our jasmine test code which will be used to test our "Add" function In the Add.js file. The below code needs to put in a file called add-spec.js.
  • Note: - The word 'spec' needs to be added to the test file so that it can be detected by jasmine.

Sample Code:

var app=require("../Add.js");
describe("Addition",function(){
it("The function should add 2 numbers",function() {
var value=app.AddNumber(5,6);
expect(value).toBe(11);
});
});

Code Explanation:

 nodejs add function

Learn Node js - node js Tutorial - nodejs add function - node - Node js Examples

  1. We need to first include our Add.js file so that we can test the 'AddNumber' function in this file.
  2. We are now creating our test module. The first part of the test module is to describe a method which basically gives a name for our test. In this case, the name of our test is "Addition".
  3. The next bit is to give a description for our test using the 'it' method.
  4. We now invoke our Addnumber method and send in 2 parameters 5 and 6. This will be passed to our Addnumber method in the App.js file. The return value is then stored in a variable called value.
  5. The final step is to do the comparison or our actual test. Since we expect the value returned by the Addnumber function to be 11, we define this using the method expect(value).toBe(the expected value).

Output:

 nodejs add function output

Learn Node js - node js Tutorial - nodejs add function output - node - Node js Examples

  1. In order to run the test, one needs to run the command jasmine.
  2. The below screenshot shows that after the jasmine command is run , it will detect that there is a test called add-spec.js and execute that test accordingly. If there are any errors in the test, it will be shown accordingly.

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Unit Testing | Node.js Unit Testing Tutorial with Jasmine