![]() |
VOOZH | about |
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:
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.
//This is test suite
describe("Test Suite", function() {
//...
});//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
npx jasmine init
Implementation Jasmine Tool: The implementation of Jasmine includes various ways, which are described below:
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.
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:
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
$ mkdir jasmine-project $ cd jasmine-project
Example:
where,
Example: This example describes the usage of the Jasmine Tool.
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:
Disadvantages of Jasmine Tool: Although there are many advantages of using Jasmine, there are drawbacks too. Those are:
Reference: https://jasmine.github.io/