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>
<script src="src/Player.js"></script>
<script src="src/Song.js"></script>
<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/
Similar Reads
What is React Developer Tool?
React Developer Tools, or React DevTools, is a Chrome DevTools extension designed to help developers debug and inspect React applications. Built specifically for React, this tool provides an in-depth view of the components in a React app, allowing developers to monitor the component tree, inspect pr
5 min read
What is EJS Template Engine ?
EJS Template Engine or EJS is a popular template engine for web development. it is used in Nodejs. It allows you to generate dynamic content by embedding JavaScript to HTML content. EJS serves as a templating language, utilizing plan javascript to HTML. It allows the injection of data into the templ
3 min read
Foundation CSS Tooltip
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails to look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, M
7 min read
Selenium Tool Suite
Selenium is a very well-known open-source software suite, mainly used for testing web browsers and web applications by automating some processes. It comes with a set of tools and libraries that allow developers or testers to automate some functions related to web browsers and web applications. Selen
7 min read
What is Cypress Test Runner?
Cypress Test Runner is a powerful, open-source testing framework designed for end-to-end testing of modern web applications. Known for its user-friendly interface and fast execution speed, Cypress allows developers and QA engineers to write, run, and debug tests directly in the browser. With built-i
7 min read
Kali Linux Tools
Kali Linux is a Linux based operating system, mostly used in penetration testing. Kali.org has recently released its new update with some extra functionalities. There are different types of tools that are present in Kali Linux to perform different operations. Types of tools in Kali Linux Information
7 min read
Materialize CSS Tooltips
Tooltips are small, textual but interactive hints that are used mainly for the graphical purpose. It helps the icons or buttons to give clarification of its function. To use this, tooltipped class is added along with its position such as: top, bottom, left or right. Syntax is as follow: <a class=
2 min read
Top 20 Test Management Tools
Test Management is a process for managing the day-to-day testing activities starting from creating the test cases to execution of the tests along with capturing the test results. Test Management tools help to manage the creation of test cases, execution of tests, storing the results for executed tes
15 min read
Top 10 Codeless Testing Tools
Software Testing plays a crucial role in today's world, where multiple technologies are being developed. These technologies, which are being developed, need proper testing to work seamlessly. Hence, codeless testing helps developers test websites, desktop applications, and mobile applications withou
8 min read
What is Nikto and it's usages ?
Nikto is an Open Source software written in Perl language that is used to scan a web-server for the vulnerability that can be exploited and can compromise the server. It can also check for outdated version details of 1200 server and can detect problems with specific version details of over 200 serve
2 min read