Book Image

Jasmine Cookbook

By : Munish Kumar
Book Image

Jasmine Cookbook

By: Munish Kumar

Overview of this book

Table of Contents (16 chapters)
Jasmine Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Writing your first Jasmine test


To write our first Jasmine test, we will use a program that will calculate the factorial of a given number.

"As a user, I want to validate/evaluate factorial functionality so that I can get the exact factorial value for a given number."

Let's consider some scenarios in the current context:

  • Scenario-1: Factorial value should be evaluated for a given positive number

  • Scenario-2: A 'null' value should be returned for negative numbers

Getting ready

To start writing Jasmine tests, you need to download Jasmine from the official website. Download the Jasmine Standalone release (that is, jasmine-standalone-2.x.x.zip) from the following website:

https://github.com/pivotal/jasmine/releases

How to do it...

To start a Jasmine test, perform the following steps:

  1. First, you need to create a spec file under the /spec folder. Create the Factorial_spec.js file and code the following lines:

    describe("Factorial", function() {
    
    });
  2. Next, add the following code to Factorial_spec.js:

    describe("Factorial", function() {
        it("should get factorial of given number", function() {
    
        });
      });
  3. Further, add another it block to your Jasmine test and use the following code:

    describe("Factorial", function() {
        it("should get factorial of given number", function() {
    
        });
        it("should return null value for passing negative number or less/more than one argument", function() {
    
        });
      });
  4. To implement the factorial functionality, we need JavaScript code. So, create a Factorial.js file and put it under the /src folder:

    function factorial(num)
    {
      if (num==1 && factorial.arguments.length == 1) {
        return 1;
      }
      else if (num >1 && factorial.arguments.length == 1) {
        return num*factorial(num-1);
      }
      else {
        return null;  /* Validate if parameter is passed as negative number or less/more than one parameter */
      }
    }
  5. Now, to test the Factorial functionality, we will implement it in the Jasmine test using the following code:

    describe("Factorial", function() {
        it("should get factorial of given number", function() {
          expect(factorial(3)).toEqual(6);
        });
        it("should return null value for passing negative number or less/more than one arguments", function() {
          expect(factorial(-3)).toEqual(null);
        });
    });

    In the preceding code snapshot, notice that we implemented both scenarios with the help of assertions using expectations and matchers.

    Note

    An expectation in Jasmine is an assertion that is either true or false.

    Jasmine provides the expect function to test the code. It accepts a value called the actual. Furthermore, it is chained with the matcher function, which accepts the expected value. In current context, the matcher function is toEqual.

    To learn more about expectation and matchers, refer to the recipes Adding expectations and matchers to the test and Applying different matchers to the Jasmine test in this chapter.

  6. To run Jasmine's spec for both scenarios, we need to add the reference of the JavaScript file (that is, Factorial.js) and spec file (that is, Factorial_spec.js) to the test runner (that is, SpecRunner.html file). Use the following code:

    <!-- include source files here... -->
      <script type="text/javascript" src="src/Factorial.js"></script>
    
    <!-- include spec files here... -->
      <script type="text/javascript" src="spec/Factorial_spec.js"></script>

    Note

    In your test runner (that is, SpecRunner.html), you can see the reference to different source files (that is, Player.js and Sonj.js) and spec files (that is, SpecHelper.js and PlayerSpec.js). These files are shipped along with the Jasmine standalone release. Here, you need to remove the references to these files and add the reference to your source file (that is, Factorial.js) and spec file (that is, Factorial_spec.js).

  7. Now, execute the test suite by opening the Jasmine runner (that is SpecRunner.html) in a browser, and you will see something similar to the following screenshot, which will let you know that the tests have run successfully:

    The SpecRunner.html file acts as a test runner for Jasmine tests. We can also call it the Jasmine runner. Indeed, from now onwards, throughout this book, we will refer to the SpecRunner.html file as the Jasmine runner.

How it works...

Let's take a look at what we did in this recipe.

In step 1, we defined the test suite using the describe function. It is a global Jasmine function and accepts the following two parameters:

  • String: Usually, we mention this parameter as the name of the test suite corresponding to the functionality that is currently being tested. A test suite represents a specific functionality or component of our application. In our case, it is Factorial.

  • Function: This is a block of code that implements the suite. You can create n suites corresponding to functionality/application components, as needed.

In step 2, we defined the condition of scenario 1 by implementing the it block inside the describe block. It's also a global Jasmine function that accepts two parameters, that is, a string and a function:

  • String: This is the title of the spec. In our case, we created the spec as should get factorial of given number for scenario 1.

  • Function: Here, we write Jasmine code (test code or the actual 'spec'), corresponding to the spec title to test the JavaScript code.

In step 3, we defined another condition for scenario 2 by adding a second spec using an additional it block. You can define as many specs within a test suite as your test requires.

In step 4, to test the JavaScript code, we created the Factorial.js file and wrote code for factorial functionality. We defined the conditions for scenario 1 and scenario 2 in the factorial function.

In step 5, we passed a value to the expect (that is, the expectation) function, which needs to be tested; this value is called the actual value. Furthermore, the expect function is chained to the matcher function, which takes the expected value. On the basis of actual versus expected value, it reports to Jasmine that the spec has either passed or failed.

In step 6, to test and execute the JavaScript code, we included the reference of the JavaScript file (Factorial.js) and the corresponding spec file (Factorial_spec.js) in the SpecRunner.html file.

See also

  • To understand more about specs and how to apply expectations and matchers, refer to the recipes Adding specs to your Jasmine test and Adding expectations and matchers to the test.

    Note

    Jasmine is a Behavior-Driven Development (BDD) framework. However, for now, we are not following the BDD process to write Jasmine tests as it is outside the scope of this recipe. In Chapter 2, Jasmine with TDD and BDD Processes, we will discuss in detail how Jasmine test and application code is developed alongside using Test-Driven Development (TDD) and BDD process.