Unit Testing Using Jasmine

Introduction

 
For those who are very new to JavaScript, it is a programming language similar to any other programming language like Java, C#, Python, or PHP. Using JavaScript, we can develop any application. Before Node.js or npm came into the picture, JavaScript was able to run only in the browser, i.e., JavaScript was the only client-side scripting language but this was the picture in 2000. Today, JavaScript has evolved a lot. Today one can develop not only client side script but we can do some complex server-side script as well with very good performance. 
 
This is possible because of one thing, that is node js. Node.js or node is a framework which we need to install on our local machine so that we can easily execute JavaScript code locally. To understand more on node.js, please visit nodejs.org. Here, you can find the latest NodeJS installer that you can easily download and install in a couple of steps.
 
After the node gets installed successfully, you can use node or npm command on the command prompt.
 
If someone is very new to JavaScript, then please go through the below links to get a good understanding of it.
  • https://www.w3schools.com/js/
  • https://developer.mozilla.org/bm/docs/Web/JavaScript
  • https://www.tutorialspoint.com/JavaScript/
  • https://nodejs.org/en/

Some background of unit testing

 
As per Wiki, unit testing is a software testing method by which the individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. In simple words, whatever functionality or code the developer has written, he/she needs to add some test cases which will validate that those lines of code are giving the right result.
 
JavaScript Unit testing - Now, we can move to JavaScript frameworks for unit testing like jasmine, mocha, and jest. These are not only three frameworks available but these three are very popular among many more frameworks.
 
Jasmine is very first we can say. To get more information, let's visit here.
 
First, we will create a sample and run so that we can get more information.
 
Unit Testing Using Jasmine
  • Create a sample project
  • Create a file as jasmine.json
  • Create the specs folder
  • Inside specs folder, create a file named SampleSpec.js
jasmine.json
  1. {    
  2.     "spec_dir""spec",    
  3.     "spec_files": ["**/*[sS]pec.js"],    
  4.     "stopSpecOnExpectationFailure"false,    
  5.     "random"true    
  6. }   
SampleSpec.js
  1. describe("Calculator Application"function() {    
  2.     it("should be able to show 2 = 2"function() {    
  3.         expect(2).toEqual(2);    
  4.     });    
  5.     it("should be able to show 3 = 1 + 2"function() {    
  6.         expect(3).toEqual(1 + 2);    
  7.     });    
  8. });   
Run this file using the command jasmine spec/SampleSpec.js
 
If this command is not working, this means jasmine is not installed on your machine; so, please install that using the command npm install -g jasmine
 
Jasmine provides some hook to where we can store some common code or functionality that will execute before and after executing each test case.
  1. beforeEach(() => {    
  2.     ···})    
  3. afterEach(() => {    
  4.     ···})   
Jasmine provides the below expected conditions so that we can easily compare the expected and actual results.
  1. expect(true).toBe(true)    
  2. expect(true).not.toBe(true)    
  3.     
  4. expect(a).toEqual(bar)    
  5.     
  6. expect(message).toMatch(/bar/)    
  7. expect(message).toMatch('bar')    
  8.     
  9. expect(a.foo).toBeDefined()    
  10. expect(a.foo).toBeUndefined()    
  11. expect(a.foo).toBeNull()    
  12.     
  13. expect(a.foo).toBeTruthy()    
  14. expect(a.foo).toBeFalsy()    
  15.     
  16. expect(message).toContain('hello')    
  17.     
  18. expect(pi).toBeGreaterThan(3)    
  19. expect(pi).toBeLessThan(4)    
  20. expect(pi).toBeCloseTo(3.1415, 0.1)    
  21.     
  22. expect(func).toThrow()   
JASMINE FOR NODE.JS
 
Add Jasmine to your package.json.
 
npm install --save-dev jasmine
 
Initialize Jasmine in your project.
 
node node_modules/jasmine/bin/jasmine init
 
Set Jasmine as your test script in your package.json
 
"scripts": { "test": "jasmine" }
 
Run your tests
 
npm test
 
Also, Jasmine provides some commands to create the folder structure and
 
Create one folder sample-app
 
npm install -g jasmine
 
-g is used to globally install a node package
 
Init a Project
 
Initialize a project for Jasmine by creating a spec directory and configuration in JSON for you.
 
jasmine init
 
To develop a JavaScript application, we need an IDE. I am using Visual Studio Code a free IDE. If you don’t have, then please download and install it on your local machine.
Note that if you have installed Jasmine locally, you could still use the command line like this,
 
node node_modules/jasmine/bin/jasmine init
 
Generate example spec and source files
  • jasmine examples
  • Running tests
  • jasmine
Using jasmine command we are able to run all test cases.
 
For some more information and updates please visit here.