Categories: Javascript

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

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.

Share
Published by
Ajitesh Kumar
Tags: javascript

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

6 days ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 weeks ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago