Game Changing Mobility

Jasmine is a behavior-driven development framework, used for testing JavaScript code. To know about behavior-driven development, we must know about test-driven development. Test-driven development, as per definition, is a software development process that relies on the repetition of a very short development cycle: first the developer writes an automated test case that defines a desired improvement or new function, then produces a minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. Behavior-driven development on the other hand, is a software development process that is emerged from test-driven development. Behavior-driven development process can be called as a combination of general techniques and principles of test-driven development.

Jasmine is an independent software as it does not depend on any other software development frameworks. It does not require a Document Object Model (DOM). A basic advantage that can be called of Jasmine is that its syntax is so obvious that it’s easy to understand. It also helps you to write your tests easily.

Work with Jasmine

Function:

Let’s start with an example code that you want to test using Jasmine. We all know, in every programming language, we start with a Hello World program. Here also, let’s begin with a helloworld.js. A JavaScript function that returns “Hello World”.

Spec:

Initially, you need to grab the latest standalone version of Jasmine on your computer. They are easily available in google. All you need to do is search for it and download it. Unzip the downloaded file. The /src and /spec directories will have many files in it. You’ll have to empty them out as they are just examples which you probably won’t require. Now, the function or let’s call the helloworld.js file should be put into the /src directory. We now have created the src. What we have to do next is to create the spec.

The code has two parts. The ‘describe’ part and the ‘it’ part. The ‘describe’ part will contain the main codes or functions that does the tests. ‘Describe’ is followed by the suite which is just English language and not any code, which helps to understand what it describes. Inside of ‘describe’, you have the ‘it’ part of the code. ‘It’ part is generally called as spec. The whole code might look like classes written in a function. ‘It’ describes what the code must do in general English language and in JavaScript code. You can have any number of specs in a suite.

Matchers:

When a code is needed to be tested, you will definitely require a matcher. A matcher is something that will do the checking whether your code provides the required output. For beginners, we can use expect() and toEqual() as matchers. In our Hello World example program, we need the code to return the expected output – “Hello World”. To test this, the matchers will run as expect (helloworld()).toEqual(“Hello World”); If that comes true, the program is successfully tested with no errors reported. There are many other matchers too. Matchers are selected according to the requirement of what is to be tested and what should be returned. In addition to this, you can make your own matchers too.

Jasmine is independent. It does not depend on any other JavaScript frameworks.

It does not require a DOM.

It has a clean, obvious syntax.

Help maintainers understand the intention behind the code.

Brings validation and proper data handling concerns to forefront.

Conclusion:

There is a plenty more you can do with Jasmine. Overall, Jasmine makes your testing fun. So if you’re not yet into testing, now is an excellent time to start your JavaScript testing. It makes your testing pretty simple with Jasmine’s fast and simple syntax.