Why Testing with Jasmine is Fun?


What is Jasmine?

Jasmine is a behaviour-driven development framework, used for testing JavaScript code. To know about behaviour-driven development, we must know about test-driven development.

Jasmine behavior-driven development framework used for testing JavaScript code

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.

Behaviour-driven development, on the other hand, is a software development process that is emerged from test-driven development. The behaviour-driven development process can be called as a combination of general techniques and principles of test-driven development.

Why use Jasmine for testing?

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.

Working with Jasmine


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”.


Initially, you need to grab the latest standalone version of Jasmine on your computer. They are easily available on 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 must 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 do the tests. ‘Describe’ is followed by the suite, which is just the 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 a ‘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.


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.

Example code

describe(“The ‘toEqual’ matcher”, function() {
it(“works for simple literals and variables”, function() {
var a = 12;
it(“should work for objects”, function() {
var foo = {
a: 12,
b: 34
var bar = {
a: 12,
b: 34
it(“The ‘toMatch’ matcher is for regular expressions”, function() {
var message = “foo bar baz”;
it(“The ‘toBeDefined’ matcher compares against `undefined`”, function() {
var a = {
foo: “foo”
it(“The `toBeUndefined` matcher compares against `undefined`”, function() {
var a = {
foo: “foo”
it(“The ‘toBeNull’ matcher compares against null”, function() {
var a = null;
var foo = “foo”;
it(“The ‘toBeTruthy’ matcher is for boolean casting testing”, function() {
var a, foo = “foo”;
it(“The ‘toBeFalsy’ matcher is for boolean casting testing”, function() {
var a, foo = “foo”;
it(“The ‘toContain’ matcher is for finding an item in an Array”, function() {
var a = ["foo", "bar", "baz"];
it(“The ‘toBeLessThan’ matcher is for mathematical comparisons”, function() {
var pi = 3.1415926,
e = 2.78;
it(“The ‘toBeGreaterThan’ matcher is for mathematical comparisons”, function() {
var pi = 3.1415926,
e = 2.78;
it(“The ‘toBeCloseTo’ matcher is for precision math comparison”, function() {
var pi = 3.1415926,
e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
it(“The ‘toThrow’ matcher is for testing if a function throws an exception”, function() {
var foo = function() {
return1 + 2;
var bar = function() {
return a + 1;
it(“The ‘toThrowError’ matcher is for testing a specific thrown exception”, function() {
var foo = function() {
thrownew TypeError(“foo bar baz”);
expect(foo).toThrowError(“foo bar baz”);
expect(foo).toThrowError(TypeError, “foo bar baz”);

(Code was taken from http://jasmine.github.io/2.0/introduction.html)

Some advantages of using Jasmine

  • 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 the forefront.


There is 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.

– Shekhar R

Leave a Reply Blue line
Your email address will not be published.
Notify of
Inline Feedbacks
View all comments