Open In App

What is Jasmine Tool ?

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Jasmine is a powerful open-source unit testing behavior-driven development framework for JavaScript and has the capability for testing any kind of JavaScript application. It has been around since 2010. It can test both synchronous and asynchronous JavaScript code. It is independent of any other JavaScript framework. Jasmine tool is mainly used by Angular developers, it is because Jasmine is included natively in Angular projects.

Features of Jasmine Tool:

  • Jasmine is available for both browsers and Node, which provides a clean syntax for the ease of writing tests.
  • Jasmine also provides behavior-driven testing and does not require any DOM(Document Object Model) for writing test cases.
  • Jasmine supports asynchronous testing.
  • For implementing test doubles, Jasmine makes use of ‘spies’.
  • Jasmine is used by the languages like Python and Ruby.
  • Jasmine is a batteries-included library that offers everything that we need for testing our code.

Working of Jasmine Tool: Jasmine is a JavaScript framework that makes our code so easy to read. In Jasmine, to understand its working, we should understand its two important terms: suite and spec.

  • Suite: A group of test cases is called a Jasmine suite. It is used for the testing purpose of the specific action/behavior of the JavaScript code(it can be either a JavaScript object or function). Its global function describes two parameters – 1st parameter shows the Title of the test suite & the 2nd parameter shows the function that implements the test suite.
//This is test suite

describe("Test Suite", function() {
    //...
});
  • Spec: We can define a Jasmine spec as a “test case inside the test suite. Its global function describes two parameters: 1st parameter shows the Title of the test suite & the 2nd parameter shows the function that implements the test suite. It contains one or more expectations which is basically an assertion that can be either true or false.
//This is test suite

describe("Test Suite", function() {
    it("test spec", function() {
      expect( expression ).toEqual(true);
    }); 
});

Installation of Jasmine Tool: We can install Jasmine using the following command locally in our project:

npm install --save-dev jasmine

We can invoke the CLI tool using “npx jasmine …” commands, with the above local installation of Jasmine. On the other hand, we can install Jasmine globally, so that we can invoke the CLI tool without npx. But installing Jasmine globally is not recommended, as it is difficult to keep the globally installed jasmine version in sync with each project that uses it.

npm install -g jasmine
  • Init a Project: We can Initialize a project for Jasmine in a very simple way. This is done by simply creating a spec directory and configuration JSON for you:
npx jasmine init

Implementation Jasmine Tool: The implementation of Jasmine includes various ways, which are described below:

  • Implementing/Using Jasmine as a Library.
  • Using Standalone Jasmine(by including the Jasmine Core & test file using the <script> tag).
  • Implementing/Using Jasmine via the CLI.

We will discuss all the above-mentioned topics in detail.

Implementing/Using Jasmine as a Library: Jasmine can be used as a Library in our project using various methods and paths.

Example: In this example, First we need to import(require) Jasmine. Then, we use the loadConfigFile() method to load the config file available from the “spec/support/jasmine.json” path, and then finally we will execute Jasmine.

Javascript




var Jasmine = require('jasmine');
var jasmine = new Jasmine();
  
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.configureDefaultReporter({
    showColors: false
});
jasmine.execute();


Using Standalone Jasmine(by including the Jasmine Core & test file using the <script> tag): For this Implementation, First we need to download the latest version of Jasmine from the GitHub link and then extract the zip file in the folder of the project you want to test. The content of the folder contains a bunch of default files and folders, which are described below:

/lib: It contains the core Jasmine files.

/src: It contains the source files that we want to test. These files also includes
 deleted files if we already have our project's folder setup or can also be used
 when appropriate for hosting our source code.
 
/spec: It contains the tests that we are going to write.
 
SpecRunner.html: This file is used as a test runner. We can run our specs by simply 
launching this file.

We need to change the files included from the /src and /spec folders to contain our actual source and test files in a default SpecRunner.html file. 

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>Jasmine Spec Runner v3.2.1</title>
  
    <link rel="shortcut icon" 
          type="image/png" 
          href="lib/jasmine-3.2.1/jasmine_favicon.png">
    <link rel="stylesheet" 
          href="lib/jasmine-3.2.1/jasmine.css">
  
    <script src="lib/jasmine-3.2.1/jasmine.js"></script>
    <script src="lib/jasmine-3.2.1/jasmine-html.js"></script>
    <script src="lib/jasmine-3.2.1/boot.js"></script>
  
    <!-- include source files here... -->
    <script src="src/Player.js"></script>
    <script src="src/Song.js"></script>
  
    <!-- include spec files here... -->
    <script src="spec/SpecHelper.js"></script>
    <script src="spec/PlayerSpec.js"></script>
  
</head>
  
<body>
</body>
  
</html>


Implementing/Using Jasmine via the CLI: Jasmine CLI allows us to run Jasmine tests with ease. By default, the output runs in the terminal. To understand in a better way and in deep first we need to run the following command to install Jasmine globally:

npm install -g jasmine
  • The next step is creating a folder for our project and navigating inside it:
$ mkdir jasmine-project $ cd jasmine-project
  • Jasmine.json file is created. This is the content of a default jasmine.json file:

Example:

Javascript




{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}


where,

  • spec_dir: This specifies the path where Jasmine looks for test files.
  • helpers: This specifies the path where Jasmine looks for helper files. These Helper files are executed before specs/ These helper files can be used to define custom matchers.
  • spec_files: This specifies the patterns of test files, which means by default all JS files that end with Spec or spec strings.
  • stopSpecOnExpectationFailure: This when set to true will immediately stop a spec on the first failure of an expectation (can be used as a CLI option via –stop-on-failure).
  • random: When set to true Jasmine will pseudo-randomly run the test cases (can be used as a CLI option via –random).

Example: This example describes the usage of the Jasmine Tool.

Javascript




describe("Nested Describe Demo", function () {
    beforeEach(function () {
        console.log("beforeEach level 1");
    });
    describe("MyTest level2", function () {
        beforeEach(function () {
            console.log("beforeEach level 2");
        });
        describe("MyTest level3", function () {
            beforeEach(function () {
                console.log("beforeEach level 3");
            });
            it("is a simple spec in level3", function () {
                console.log("A simple spec in level 3");
                expect(true).toBe(true);
            });
            afterEach(function () {
                console.log("afterEach level 3");
            });
        });
        afterEach(function () {
            console.log("afterEach level 2");
        });
    });
    afterEach(function () {
        console.log("afterEach level 1");
    });
});


On executing the corresponding HTML file in the browser, we can observe the console output, which is written as:

Output: In the above code, describe function is used for grouping related specs. Whereas the string parameter is used for naming the collection of specs(spec represents a test case inside the test suite). 

beforeEach level 1 
beforeEach level 2 
beforeEach level 3 
A simple spec in level 3 
afterEach level 3 
afterEach level 2 
afterEach level 1

Advantages of Jasmine Tool:

  • Jasmine is browser, framework, platform, and language independent.
  • It provides a clean and readable syntax for the ease of writing tests using their built-in matchers too.
  • Jasmine supports both test-driven development and behavior-driven development.

Disadvantages of Jasmine Tool: Although there are many advantages of using Jasmine, there are drawbacks too. Those are:

  • It requires plenty of configurations.
  • The user who uses the Jasmine tool must select an assertion library or a mocking framework before using it. 
  • The process of configuration can be quite frustrating.

Reference: https://jasmine.github.io/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads