Javascript – Jasmine Unit Tests Explained with Code Samples

This article represents instructions and code samples on how to setup Jasmine along with unit tests code samples. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the key points described later in this article:

  • Set up Jasmine Development Environment
  • Set the Jasmine Config Property
  • Create Javascript Files including Jasmine.json, JS Modules & Unit tests
Setup Jasmine Development Environment

I would suggest setting up a Docker image for Javascript environment consisting of key tools such as grunt, jscs, jshint, typescript, browserify, nodejs etc. Check my page on How to setup Javascript Dev Environment using Dockers.

Set the Jasmine Config Property

Set the Jasmine config property (environment variable) pointing to folder where jasmine.json file can be found. As I am executing jasmine from topmost folder (/home/ashukla/js) under which jasmine.json is found, I set the path by executing “export JASMINE_CONFIG_PATH=/home/ashukla/js/jasmine.json”.

Create Javascript Files including Jasmine.json, JS Modules & Unit tests

Make a directory under with following subfolders and a configuration file will be placed:

  • jasmine.json: This is a configuration file which defines the path where unit tests saved in ***Spec.js file will be found. Following is how the default jasmine.json file looks like:
    {
      "spec_dir": "spec",
      "spec_files": [
        "**/*[sS]pec.js"
      ],
      "helpers": [
        "helpers/**/*.js"
      ],
      "stopSpecOnExpectationFailure": false,
      "random": false
    }
    
  • lib: This folder will consist of JS files to be tested. In this example, I have created a class/module named as Person.js. Following is the sample code:
    function Person(){
        this.age = 0;
        this.isAdult = false;
    };
    
    Person.prototype.setAge = function(age) {
        this.age = age;
        if(this.age > 17) {
            this.isAdult = true;
        } else {
            this.isAdult = false;
        }
    };
    
    module.exports = Person;
    
  • spec: This folder will consist of unit tests files. I have created a unit test file such as PersonSpec.js which consists of unit tests to test the Person.js module. Following is the code sample:
    describe("Person", function() {
      var Person = require('../lib/Person');
      var person;
    
      beforeEach(function() {
        person = new Person();
      });
    
      it("is not an adult", function() {
          expect(person.isAdult).toBe(false);
          person.setAge(16);
          expect(person.isAdult).toBe(false);
      });
    
      it("is an adult", function() {
          person.setAge(18);
          expect(person.isAdult).toBe(true);
      });
    
    });
    
    

 

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Javascript. Tagged with .