What is the Jasmine Framework Used for?

Testing is a key element to any application. For NodeJS unit testing, 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 Jasmine tutorial, you will learn how to get your environment setup with Jasmine and how to run Jasmine tests for your first Node.js application. In this Jasmine testing tutorial, you will learn-

Overview of Jasmine for testing Node.js applications How to use Jasmine to test Node.js applications

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.

How to Setup Jasmine Test Environment

Next in this Jasmine Node JS tutorial, 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) Install NPM Modules You 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) Initialize the Jasmine Environment 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.

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.

Next in this Jasmine framework tutorial, we will learn how to use Jasmine to test Node.js applications.

How to use Jasmine to test Node.js applications

In order to use Jasmine unit testing for 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.”

Code Explanation:

The “exports” keyword is used to ensure that the functionality defined in this file can actually be accessed by other files. 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. We are finally making our function return the added value of the parameters.

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:

We need to first include our Add.js file so that we can test the ‘AddNumber’ function in this file. 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”. The next bit is to give a description for our test using the ‘it’ method. 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. 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

In order to run the test, one needs to run the command jasmine. 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.

Summary

In order to test a Node.js application, the jasmine framework needs to be installed first. This is done by using the Node package manager. The test code needs to be written in a separate file, and the word ‘spec’ should be appended to the file name. Only if this is done will jasmine be able to detect that a file needs to be run. To run the test, you need to execute the jasmine command. This will find all files which have the ‘spec’ word attached to it and run the file accordingly.