Node-js
Node.Js Vs AngularJS: Know the Difference
What is Node JS? Node.js is a cross-platform runtime library and environment for running...
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.
In this tutorial, you will learn-
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 shows how to setup Jasmine in your environment
Step 1) Installing the NPM Modules
You to need to install jasmine module to use the jasmine framework from within a Node application. To install the jasmine-node module, run the below command.
npm install jasmine-node
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.
In order to use Jasmine to test Node.js applications, a series of steps need to be followed.
In our example below, we are going to define a module which adds 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."
var exports=module.exports={}; exports.AddNumber=function(a,b) { return a+b; };
Code Explanation:
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.
Code Explanation:
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); }); });
Output
Summary
What is Node JS? Node.js is a cross-platform runtime library and environment for running...
Introduction to Node.js The modern web application has really come a long way over the years with...
In this tutorial, we will study the Express framework. This framework is built in such a way that...
To start building your Node.js applications, the first step is the installation of the node.js...
Bluebird is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is...
In previous tutorials, you would have seen callback functions which are used for Asynchronous...