Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Jasmine Cookbook

You're reading from   Jasmine Cookbook Over 35 recipes to design and develop Jasmine tests to produce world-class JavaScript applications

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781784397166
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Munish Kumar Munish Kumar
Author Profile Icon Munish Kumar
Munish Kumar
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting Started with Jasmine Framework 2. Jasmine with TDD and BDD Processes FREE CHAPTER 3. Customizing Matchers and Jasmine Functions 4. Designing Specs from Requirement 5. Jasmine Spies 6. Jasmine with AJAX, jQuery, and Fixtures 7. Code Coverage with Jasmine Tests 8. Jasmine with Other Tools 9. Developing JavaScript Apps Using Jasmine – a Real-time Scenario Index

Using the "this" keyword

In Jasmine, we can also initialize and share the variables between beforeEach, it, and afterEach using the this keyword. In this recipe, we will see how the this keyword can be used within the beforeEach or afterEach functions.

Getting ready

You will learn this recipe with the help of the previous recipe. For more information, refer to the previous recipe, Applying setup and teardown functions to the Jasmine test. In this recipe, we refactor three scenarios for a sales and inventory control system by initializing/putting together common code in the beforeEach and afterEach functions.

How to do it…

To apply the this keyword to your Jasmine test, you need to perform the following steps:

  1. First, you need to create a spec file (InventoryStockOptimizeCode_With_this_spec.js) under the /spec folder and get the following code from the spec file (InventoryStockOptimizeCode_spec.js) created in the previous recipe, Adding specs to your Jasmine test:
    describe("Inventory Stock", function() {
      var stockinhand_item1, item1;
      beforeEach(function() {
          stockinhand_item1=11, item1 = 1;
        });
      afterEach(function() {
          stockinhand_item1=0, item1 = 0;
        });
      //Scenario - 1
      it("Inventory Stock should be updated on sale of an item", function() {    
          expect(stockinhand_item1-item1).toEqual(10);
        });
        it("Inventory Stock should be updated on issue of an item within organization", function() {
            expect(stockinhand_item1-item1).toEqual(10);
        });
        //Scenario - 2
        it("Inventory Stock should be updated on return of an item", function() {
            expect(stockinhand_item1+item1).toEqual(12);
        });
        //Scenario - 3
        it("Inventory Stock should be updated on receiving or procuring new item", function() {
            expect(stockinhand_item1+item1).toEqual(12);
        });
    });
  2. Now, apply the this keyword by using the following code:
    describe("Inventory Stock", function() {
      beforeEach(function() {
          this.stockinhand_item1=11, this.item1 = 1;
          console.log("beforeEach: Stock in hand for item1 before spec execution = " + this.stockinhand_item1);      
        });
      afterEach(function() {
          this.stockinhand_item1=0, this.item1 = 0;
          console.log("afterEach: Stock in hand for item1 once spec executed = " + this.stockinhand_item1);
        });
    
      //Scenario - 1
      it("Inventory Stock should be updated on sale of an item", function() {    
          this.transactionType = "SALE";
          expect(this.stockinhand_item1-this.item1).toEqual(10);
          expect(this.transactionType).toBeDefined();
        });
        it("Inventory Stock should be updated on issue of an item within organization", function() {
            expect(this.stockinhand_item1-this.item1).toEqual(10);
          expect(this.transactionType).toBeUndefined();
        });
    
        //Scenario - 2
        it("Inventory Stock should be updated on return of an item", function() {                expect(this.stockinhand_item1+this.item1).toEqual(12);
          expect(this.transactionType).toBeUndefined();
        });
    
        //Scenario - 3
        it("Inventory Stock should be updated on receiving or procuring new item", function() {        
      expect(this.stockinhand_item1+this.item1).toEqual(12);
      expect(this.transactionType).toBeUndefined();
        });
    });
  3. Finally, run the spec file (InventoryStockOptimizeCode_With_this_spec.js) using the Jasmine runner (that is, SpecRunner.html). You should see test execution results, as shown in the following screenshot, indicating that all four tests are passing:
    How to do it…

How it works...

In steps 1 to 3, we looked at how the this keyword is applied to a Jasmine test. In step 2, we removed the variable declaration from the top-level scope (that is, the describe block) and initialized/reinitialized the variables into the beforeEach/afterEach functions using the this keyword. Also, notice that this.transactionType is true only for the spec in which it was defined. For the other specs, the this.transactionType variable is considered as undefined. In other words, the scope of the this.transactionType variable is limited to the first spec only (that is, Inventory Stock should be updated on sale of an item). Conversely, the this.item1 variable is considered defined for all the specs because it is assigned in the beforeEach function that runs each time.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image